Re: Bug with Visual Studio Optimizer?
Ulrich Eckhardt wrote:
:: Jason Hise wrote:
::: The following code prints GOOD in debug mode, and BAD in release
::: mode. Is that allowed by the standard? This was the root cause
::: of a very difficult to hunt down bug in my program, where I was
::: using the function addresses as keys into a map. I am wondering
::: if I should submit a bug report to Microsoft.
:::
::: #include <iostream>
:::
::: template<typename Type>
::: void foo() { }
:::
::: int main()
::: {
::: std::cout << (&foo<int> == &foo<float>
::: ? "BAD" : "GOOD") << std::endl;
::: return 0;
::: }
::
:: Just wondering, you aren't using that ten year old, unsupported and
:: deprecated VC6, are you? In that case, this behaviour is a known
:: bug and you're not going to get it fixed except by upgrading.
::
:: The problem there seems to boil down to VC6 not mangling the
:: template parameters into the name. The typical workaround looks
:: like this:
::
:: #if defined(_MSC_VER) && _MSC_VER < 1300
:: template<typename T>
:: void foo( T* = 0)
:: #else
:: ... // normal C++ code
:: #endif
::
:: However, in that case your code will stop compiling because it
:: compares pointers to functions with different arguments.
::
And if it is a later version of VC++, there is another quirk - that
templates generating identical code are merged. The compiler generates
code for a template each time it is instantiated. The linker then
collects all the copies, and discards all but one.
It shouldn't do that when a function's address i taken, but it does.
This is a well known limitation of MS' template model.
If you give the different instantiations a different content, it works
template<typename Type>
void foo() { std::cout << Type(); }
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]