1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Template constructor with explicitly specified template parameter?

Discussion in 'C++' started by HighCommander4, Sep 3, 2010.

  1. HighCommander4

    HighCommander4 New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Is it possible to have a templated constructor with a template parameter that is explicitly specified (as opposed to deduced)?

    Something like this:
    Code:
    struct A
     {
         template 
         A(int x)
         {
         }
     };
    
     int main()
     {
         A a = A(5);
     }
    
    This code fails to compile, which is not surprising because A(5) is the syntax for when the template parameter belongs to the class, not the constructor.

    If this simply cannot be done, then is there a way to achieve a similar effect?
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    I am not sure if it can be done, as trying A::A<float>(5) does not work either. Why exactly do you want to do this? You may be able to use the named constructor idiom, i.e., provide a static member function template that acts as a constructor, returning an object of the class.
     

Share This Page