Почему в качестве аргумента шаблона нельзя передать адрес std::cout
? Или, если это возможно, то как?C++: & (std :: cout) в качестве аргумента шаблона
Вот что я пробовал:
#include <iostream>
template<std::ostream* stream>
class MyClass
{
public:
void disp(void)
{ (*stream) << "hello"; }
};
int main(void)
{
MyClass<&(std::cout)> MyObj;
MyObj.disp();
return 0;
}
И сообщение об ошибке я получил от clang++ -std=c++11
:
main.cpp:15:11: error: non-type template argument does not refer to any declaration
MyClass<&(std::cout)> MyObj;
^~~~~~~~~~~
main.cpp:6:24: note: template parameter is declared here
template<std::ostream* stream>
^
1 error generated.
и от g++ -std=c++11
:
main.cpp: In function ‘int main()’:
main.cpp:15:22: error: template argument 1 is invalid
MyClass<&(std::cout)> MyObj;
^
main.cpp:15:29: error: invalid type in declaration before ‘;’ token
MyClass<&(std::cout)> MyObj;
^
main.cpp:16:8: error: request for member ‘disp’ in ‘MyObj’, which is of non-class type ‘int’
MyObj.disp();
^
Любые идеи?
Почему вы заключили в скобки его? Также установите уровень доступа. – LogicStuff
Попробуйте это без круглых скобок: MyClass <&std::cout> MyObj; ' – aschepler
Я бы хотел получить объяснение, как это разбирается. – LogicStuff