… are idiotic of the devil. Not the idea necessarily, just the details. Pop quiz:
Given this template declaration:
double foo(double);
template <class T>
class Test {
public:
void invariant() { member = foo(val); }
T dependent() { return foo(member); }
private:
int val;
T member;
};
and this instantiation:
int foo(int);
Test<int> test;
which instance of foo will be called here?
test.invariant();
test.dependent();
The answer: Both. FFFFFFFUUUUUUUUU-
Because the invocation of foo() in the method invariant() is only dependent on the type of val, which is not a template argument but the same type for all instantiations of Test<T>, it is resolved using the declaration scope to foo(double). Perfectly reasonable.
However, for some reason, the designers of C++ decided that the scope of a template
instantiation should influence the decision of how to resolve overloaded functions
inside the class.
Because the the type of the argument to foo() in dependent() is a template argument, it is unknown until instantiation time. This apparently means the compiler should fling in the scope in which Test is instantiated, just because it can't do a complete type check until it knows the type arguments. Thus, dependent() will call foo(int), which is
completely out of scope at the definition of Test.
Why, for the love of all that is sane, would you
ever mess with scope that way? Even worse, why would you ever make it
different in different situations?
Hey, I did the same thing in a quiz today. What the hell.
Ah the joys of programming.
All the more reason for a saner programming language in my opinion.I definitely don't mean C# when I say a saner language, although some people might… I also don't understand what you mean by "they're STILL using [foo()] in C++ quizzes." Are you referring to the name? Why would "they" stop using a perfectly good metasyntactic variable?
In any case, you're completely wrong about templates. They are 100% compile-time and not dynamic in any way. The only overhead they introduce at runtime is a space overhead, if you can even consider it that. Each instantiation of a template is created at compile time and becomes exactly the same code-generation wise as if you had done it all manually.It is also completely untrue that "all programmers have to deal with" "this kind of problem." Plenty of languages have generics without mangling scope- the only reason C++ programmers have to deal with this is the stupid C-macro/operationally thinking mindset of the standards committee. If both cases simply resolved to the scope of the template declaration things would be perfectly fine.Welcome to C++. This is C++. Welcome. This is C++, welcome to C++!
You can do anything with C++. Anything at all! The only limit is yourself.Welcome to C++. Welcome to C++! This is C++, welcome! Yes, this is C++, and welcome to you who uses C++!You can do anything with C++. The infinite is possible with C++. Welcome.Including shoot yourself in the foot with a million temporary bullets. :P
Yeah, there's that. Just instantiate them during war while the shoot() method isn't overloaded to spray confetti.
C++ shenanigans woo
Haha, love those. :)