Implicit Template Parameters

后端 未结 7 929
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 15:00

The following code generates a compile error in Xcode:

template 
struct Foo
{
    Foo(T Value)
    {
    }
};

int main()
{
    Foo MyFoo(123);         


        
7条回答
  •  野性不改
    2021-02-07 15:47

    It makes a lot of sense it is like this, as Foo is not a class, only Foo where T is a type.

    In C++0x you can use auto, and you can create a function to make you a Foo, let's call it foo (lower case f). Then you would do

    template Foo foo(int x)
    {
      return Foo(x);
    }
    
    auto myFoo = foo(55);
    

提交回复
热议问题