Template default arguments

前端 未结 4 1580
耶瑟儿~
耶瑟儿~ 2020-11-29 17:19

If I am allowed to do the following:

template 
class Foo{
};

Why am I not allowed to do the following in main?

相关标签:
4条回答
  • 2020-11-29 17:49

    You are not allowed to do that but you can do this

    typedef Foo<> Fooo;
    

    and then do

    Fooo me;
    
    0 讨论(0)
  • 2020-11-29 17:50

    You have to do:

    Foo<> me;
    

    The template arguments must be present but you can leave them empty.

    Think of it like a function foo with a single default argument. The expression foo won't call it, but foo() will. The argument syntax must still be there. This is consistent with that.

    0 讨论(0)
  • 2020-11-29 17:57

    You can use the following:

    Foo<> me;
    

    And have int be your template argument. The angular brackets are necessary and cannot be omitted.

    0 讨论(0)
  • 2020-11-29 18:14

    With C++17, you can indeed.

    This feature is called class template argument deduction and add more flexibility to the way you can declare variables of templated types.

    So,

    template <typename T = int>
    class Foo{};
    
    int main() {
        Foo f;
    }
    

    is now legal C++ code.

    0 讨论(0)
提交回复
热议问题