Sign in to follow this  

Templated Constructor

This topic is 4303 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

This isn't extremely important, but for future reference, how might I choose template parameters for a constructor? Example:
struct Foo
 {
  template <int bar> Foo()
   {}
 };

Foo baz<42>(); // TODO: Make this syntactically correct.

Share this post


Link to post
Share on other sites
I don't think you can. Since you don't explicitly call the constructor, there is no way to specify the template parameters. This works though:
    struct Foo
{
template < typename T >
Foo( T x ) : bar( x ) {}

int bar;
};
Foo baz( 42.5 );

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
This isn't extremely important, but for future reference, how might I choose template parameters for a constructor?


The template argument has to be able to be deduced by the constructor arguments. You can do:


template< typename Type >
struct dummy {};

class your_type
{
public:
template< typename Type >
your_type( dummy< Type > ) {}
};

int main()
{
your_type your_object( dummy< int >() );
}

Share this post


Link to post
Share on other sites

This topic is 4303 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this