I would have, but my sort() template function would have been ambiguous with std::sort() then.
Really hypothetical example:
1 2 3 4 5 6 7 8 9 10 11
|
struct frobnicate : boost::static_visitor<>
{
template< typename T >
void operator()( const T& t ) const
{ std::cout << t << << std::endl; } // As an example
};
// ...
typedef boost::variant< int, char, double, std::string > MyVariant;
MyVariant v( std::string( "Hello World" ) );
boost::apply_visitor( frobnicate(), v );
|
Suppose that I just added the std::string element to MyVariant, and I know that frobnicate
needs a specialization of operator() to handle std::string. Problem is, if I forget to add it,
the code still compiles; it just calls the templated operator() which is not what I want. I'd
like it to not compile, but the only way to do that is to eliminate the templated operator()
and write the same function three times for int, char, and double, which seems stupid
(after all, that's part of what templates are for).
In my example, the templated operator() is like std::swap().
Now this example isn't quite the same as the issue you brought up, I realize, because in my
case "std::swap<T>()" and "std::swap<std::string>()" are declared at the same point, so you
either get both or neither. The problem you raise is just another example of code that is
structured such that it is possible to have unintended or unexpected behavior. But that's
basically unavoidable in any non-trivial application.