Templated Constructor

Started by
2 comments, last by smart_idiot 18 years, 1 month ago
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.

Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
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 ); 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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 >() );}
I'm kind of disappointed, but like I said, it wasn;t important. Thanks for the info.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement