The following function template with specializations should be used within the same .cpp
file only, so I’d like to make it static
. The following code compiles (without warnings) using both MS Visual C++ 2008 and GCC 4.8.1 and works as intended. (Adding static
to the beginning of lines 5 and 11 would cause GCC to issue an error but not MSVC.)
1 template <class T>
2 static bool foo(const T param);
3
4 template <>
5 bool foo<int>(const int param)
6 {
7 return doSomethingWithInt(param);
8 }
9
10 template <>
11 bool foo<bool>(const bool param)
12 {
13 return doSomethingWithBool(param);
14 }
However, MISRA C++ checker complains:
- (MISRA2008.3-3-2) Apply the static keyword to declaration of 'foo' (1)
- (MISRA2008.3-3-2) Apply the static keyword to declaration of 'foo' (5)
- (MISRA2008.2-10-5-b) Identifier 'foo' is being reused (5)
- (MISRA2008.3-3-2) Apply the static keyword to declaration of 'foo' (11)
- (MISRA2008.2-10-5-b) Identifier 'foo' is being reused (11)
I tried to find out what’s wrong and found a hint in a C++ standard quote:
For a function call that depends on a template parameter, if the function name is an unqualified-id but not a template-id, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:
- For the part of the lookup using unqualified name lookup (3.4.1), only function declarations with external linkage from the template definition context are found.
Does it mean that the compilers discard the static
specification and there is no way to actually make static function templates in C++03?