Having the following code:
template<typename T, typename OutStream = std::ostream> struct print {
OutStream &operator()(T const &toPrint, OutStream &outStream = std::cout) const {
outStream << toPrint;
return outStream;
}
};
This call is erroneous:
print<int>(2);
Error message:
1>main.cpp(38): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'print<T>'
1> with
1> [
1> T=int
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
This call is not erroneous:
print<int> intPrinter;
intPrinter(2);
Can I use a function object somehow without its instantiation?
I cannot use a template function here, because I need partial specialization capabilities.
I think that you want to say
Here, the first parens create a temporary
print<int>object by calling the (zero-argument) constructor, then the second parens actually invoke the function call operator on that object. The error you’re getting now is caused by the fact thatIs interpreted as a typecast expression to convert 2 into a
print<int>, which isn’t what you want (and also isn’t legal).Hope this helps!