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 <iostream>
template<std::ostream* stream>
class MyClass
{
public:
void disp(void)
{ (*stream) << "hello"; }
};
int main(void)
{
MyClass<&(std::cout)> MyObj;
MyObj.disp();
return 0;
}
And the error message I got from 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.
and from 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();
^
Any ideas?
MyClass<&std::cout> MyObj;
– Augmentative&object.subobject
where it doesn't seem clear to me whether the ampersand is going to apply toobject
or tosubobject
(although I'm sure the standard must lift that ambiguity)… I do agree however that there was no such ambiguity in the present case… – Nf()
makes it an expression that requires runtime evaluation (more or less). Just throwing()
everywhere without understanding what it does is a bad idea! – Mallet&X::m
is required for pointers to member, but I can't find anything that applies here. – Augmentative