Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.SPEC.SAMEFILE

Template specialization and its primary template are declared in different files.

MISRA-C++ Rule 14-7-3 (required): All partial and explicit specializations for a template shall be declared in the same file as the declaration of their primary template.

[NDR 14.5.4(1), 14.6.4.1(7), 14.7.3(6)]

Rationale

It is undefined behaviour if, for a set of template-arguments, an implicit instantiation is generated by the compiler, and a partial or explicit specialization is declared or defined elsewhere in the program that would match the set of template-arguments.

Example

// tmpl.h
template <typename T> void bad_tmpl () {}

template <typename T> void good_tmpl () {}
template <> void good_tmpl<int32_t> () {}

// tmpl.cc
#include "tmpl.h"
template <> void bad_tmpl<int32_t> () {} // Non-compliant
template <> void good_tmpl<int32_t> () {}

// f.cc
#include <tmpl.h>

void f ()
{
   bad_tmpl<int32_t> ();  // implicit instantiation of primary.
                          // explicit instantiation in tmpl.cc would
                          // have been used if it were visible.
   good_tmpl<int32_t> (); // specialization of good_tmpl<int32_t> is
                          // visible with the primary declaration.
}