C++ : &(std::cout) as template argument

后端 未结 2 1074
长发绾君心
长发绾君心 2020-12-18 07:29

Why isn\'t it possible to pass std::cout\'s address as template argument? Or if it is possible then how?

Here is what I tried:

#include          


        
2条回答
  •  臣服心动
    2020-12-18 07:57

    This fixes your code, omit the parenthesis:

    #include 
    
    template
    class MyClass
    {
    public:
        void disp(void) { 
            (*stream) << "hello"; 
        }
    };
    
    int main(void)
    {
        MyClass<&std::cout> MyObj;
        MyObj.disp();
    
        return 0;
    }
    

    Live Demo


    A more detailed explanation why can be found here:

    Error with address of parenthesized member function

提交回复
热议问题