Today one of my friends told me that the following code compiles well on his Visual Studio 2008:
#include <vector>
struct A
{
static int const const_iterator = 100;
};
int i;
template <typename T>
void PrintAll(const T & obj)
{
T::const_iterator *i;
}
int main()
{
std::vector<int> v;
A a;
PrintAll(a);
PrintAll(v);
return 0;
}
I usually use g++, and it always refuse to pass the second PrintAll() call. As I know, for this problem, g++ is doing the standard way translating a template.
So, is my knowledge wrong, or is it a extension of VS2008?
This is not an extension at all.
VC++ never implemented the two phases interpretation properly:
VC++ never implemented the first phase... it's inconvenient since it means not only that it accepts code that is non-compliant but also that it produces an altogether different code in some situations.
void foo(int) { std::cout << "int" << std::endl; }
template <class T> void tfoo() { foo(2.0); }
void foo(double) { std::cout << "double" << std::endl; }
int main(int argc, char* argv[])
{
tfoo<Dummy>();
}
With this code:
foo
does not depend on T
.It might seem stupid as far as differences go, but if you think about the number of includes you have in a large program, there is a risk that someone will introduce an overload after your template code... and BAM :/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With