Is the compiler allowed to ignore the built-in if the template specializes?

Suppose you have a simple template function (and not a class member for simplicity) with type specificity in the same .h file ...

template <class TYPE>
void    some_function(TYPE& val)
{
    // some generic implementation
}

template <>
inline void some_function<int>(int& val)
{
    // some int specific implementation
}

If you did not explicitly point the compiler to the inlinespecialization keyword ( inline), you will receive a communication error if the .h file is included more than once (at least in Visual C ++ 2008). We all know that inlinethis is just a suggestion to the compiler that it can ignore. In this particular case, is the compiler allowed to ignore this sentence and let the linker crash?

+5
source share
5 answers

" inline".

inline, , , (ODR).

- , " inline", , . "Ignore inline" () .

, (.. " " ), - ODR. , . .

.

+5

inline, extern linkage .obj, .

, , , static, . static , - -, inline - .

+7

, , . - , . "" , . ( ), :

namespace {
    …declarations…
}

, ( ) , :

void some_other_function(int& val) {
    // some int specific implementation
}

, , , , . ( ).

+1

, extern, .cpp. - GCC, , . MSDN Magazine , .

0

, , - One Definition (ODR) , TU . , int, , . . - , . / .

inline, , , , ( ), . :

:

template<class TYPE>
void some_function(TYPE& val) {
  // some generic implementation
}

template<>
void some_function<int>(int& val);

(.cpp):

template<>
void some_function<int>(int& val) {
  // some int specific implementation
}
0

All Articles