Jump to content
  • Advertisement
Sign in to follow this  
sprite_hound

template class template constructors

This topic is 2995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The code pretty much explains itself. How do I call a value templated class template constructor?



template<class T>
class TClass {
public:
template<int S>
explicit TClass(T foo): foo(foo) {}

private:
T foo;
};

int main(int, char*[]) {
//TClass<int> t2(1); // nope (obviously)
//TClass<int> t2<5>(1); // nope
//TClass<int> t2.template TClass<5>(1); // nope
//TClass<int> t2.template TClass<int>::TClass<5>(1); // madness (nope)
TClass<int> t2.template TClass<int>::TClass<int><5>(1); // SPARTA!!!! (nope)
// Umm?
}



Share this post


Link to post
Share on other sites
Advertisement
You don't. One of the many fun parts about C++. You'll need to provide an argument for template resolution to occur with constructors. Not necessarily an S, but a type-to-type argument would work as well.

Share this post


Link to post
Share on other sites
Oh. The special C++ kind of fun. ♥


template<class T>
class TClass {
public:
template<int S>
TClass(T foo, Loki::Int2Type<S>): foo(foo) {}

private:
T foo;
};

int main(int, char*[]) {
TClass<int> t(1, Loki::Int2Type<5>()); // yay (headdesk)
}





Thanks SiCrane!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!