c++visual-studio-2010templates

Visual Studio not giving me errors because of template before each function. C++


Visual studio isn't giving me errors because of the template line before each function.

For example:

template <class keyType, class valueType>
void Map<keyType, valueType>::remove (keyType key)
{
cout<<"hello"
}

It won't give me an error for the missing semicolon on the cout.

How can i fix this? Thanks!


Solution

  • This is a well-known foible of the Visual C++ compiler. It boils down to this: Template bodies are not parsed unless and until the template is instantiated.

    When the compiler finds a template definition, it copies it into an internal buffer for future reference. Then, when that template is instantiated with concrete types, it does a search-and-replace to put the concrete types in and then parses the template.

    This has several nasty effects:

    Fair enough, once upon a time all compilers parsed templates this way. Everyone else has seen it for the maintainability and portability disaster that it is, but not Microsoft. Time to move on.