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: 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();
^
어떤 아이디어 :
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
에서
왜 괄호 안에 넣었습니까? 또한 액세스 수준을 수정하십시오. – LogicStuff
괄호없이 시험해보기 :'MyClass <&std::cout> MyObj; – aschepler
어떻게 구문 분석되는지에 대한 설명을보고 싶습니다. – LogicStuff