what's the purpose of typename assignment inside templates

前端 未结 6 918
后悔当初
后悔当初 2021-01-02 08:07

I have come across this piece of code (I\'m trying to include all details in case I\'m missing something):

template< typename TYPE = TYPE_with_an_arbitrar         


        
6条回答
  •  自闭症患者
    2021-01-02 08:25

    A function-template may not be the best construct to demonstrate default template arguments. Here's something similar with template-structs:

    #include 
    #include 
    
    template
    struct foo {
       static void f() {
          std::cout << typeid(T).name() << "\t";
       }
    };
    
    template
    struct bar {
       static void f() {
          std::cout << typeid(T).name() << "\t";
       }
    };
    
    int main() {
      foo<>::f(); foo::f();  foo::f();  std::cout << std::endl;
      bar<>::f(); bar::f();  bar::f();  std::cout << std::endl;
    }
    

    Running this, I get:

    % ./a.out 
    i   i   d   
    d   i   d   
    

提交回复
热议问题