D
DeMarcus
Hi,
Consider this example where A could be something similar to a home made
std:stream.
class A
{
public:
template<typename T>
A& operator<<( const T& t )
{
std::cout << "Function Template" << std::endl;
return *this;
}
};
// Just some class.
class B
{
};
// Similar to printing B.
A& operator<<( A& a, const B& b )
{
std::cout << "Non-member Function" << std::endl;
return a;
}
int main()
{
A a;
B b;
a << b;
}
Now, if I run all this in the same file I get the output "Non-member
function". But if I put class B and its operator<<() in a separate file
I get the output "Function Template".
Shouldn't the operator<< resolution be predictable? I want to run the
non-member function if it exists, and the template function otherwise.
How can I achieve that?
Thanks,
Daniel
Consider this example where A could be something similar to a home made
std:stream.
class A
{
public:
template<typename T>
A& operator<<( const T& t )
{
std::cout << "Function Template" << std::endl;
return *this;
}
};
// Just some class.
class B
{
};
// Similar to printing B.
A& operator<<( A& a, const B& b )
{
std::cout << "Non-member Function" << std::endl;
return a;
}
int main()
{
A a;
B b;
a << b;
}
Now, if I run all this in the same file I get the output "Non-member
function". But if I put class B and its operator<<() in a separate file
I get the output "Function Template".
Shouldn't the operator<< resolution be predictable? I want to run the
non-member function if it exists, and the template function otherwise.
How can I achieve that?
Thanks,
Daniel