Skip to main content

Secondary navigation

  • Downloads
  • Integrations
  • Blog
  • Company
    • About Us
    • Team
    • Culture
    • Careers
    • Partners
    • Press
    • Events
  • Contact
    • Contact Us
    • Request Support
    • Subscribe
  • Language
    • English
    • 日本語
    • 简体中文
    • 한국어
    • Deutsche
Home
Perforce

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • Explore Products
    • All Products

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale
    Helix Core
    Helix4Git
    Helix TeamHub
    Helix ALM
    Hansoft
    Gliffy
    Perfecto
    TestCraft
    Helix QAC
    Klocwork
    Akana
    OpenLogic
    Zend Server
    TotalView
    XRebel
    HostAccess
    SourcePro
    Stingray
    HydraExpress
    PV-WAVE
    Visualization
    IMSL
    Methodics IPLM
    VersIC
    JRebel
  • Solutions

    Main Navigation - Mega Menu

    • Explore Solutions
    • Solutions Overview

    Main Navigation - Mega Menu

    • By Need
    • By Industry

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale

    Main Navigation - Mega Menu

    • Aerospace & Defense
    • Automotive
    • Embedded Systems
    • Semiconductor
    • Energy
    • Financial
    • Game Development
    • Virtual Production
    • Government
    • Medical Devices
      Icon
    • Software

    Main Navigation - Mega Menu

    • Application Lifecycle Management
    • Agile Project Management
    • Diagramming

    Main Navigation - Mega Menu

    • DevOps
    • Version Control
    • IP Lifecycle Management
    • Java Application Development

    Main Navigation - Mega Menu

    • Web & Mobile App Testing
    • Codeless Selenium Automation
    • Static Analysis
    • Audit & Compliance

    Main Navigation - Mega Menu

    • API Management
    • Open Source Support
    • Enterprise PHP
    • HPC Debugging
    • Development Tools & Libraries
  • Customers
  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Papers & Videos
    • Recorded Webinars
    • Events
    • Blog
    • Demos
    • Subscribe
    Image VCS IPreuse Preview new

    How to Make IP Reuse Work For You

    Read Now
  • Support

    Main Navigation - Mega Menu

    • Explore Support
    • Support Plans
    • Self-Service Resources
    • Documentation
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download Software
    • Request Support

    Support

    Get answers quick by searching our public knowledgebase

    Visit Perforce KB

  • Try Free
  • Downloads
  • Integrations
  • Blog
  • Company

    Main Navigation - Mega Menu

    • About Us
    • Careers
    • Culture
    • Events
    • Partners
    • Press
    • Team
  • Contact

Version 4.0

High Integrity C++ Coding Standard

Released October 3, 2013

Request PDF Version
High Integrity C++ Coding Standard
0. Introduction
1. General
2. Lexical Conventions
3. Basic Concepts
4. Standard Conversions
5. Expressions
6. Statements
7. Declarations
8. Definitions
9. Classes
10. Derived Classes
11. Member Access Control
12. Special Member Functions
13. Overloading
14. Templates
15. Exception Handling
16. Preprocessing
17. Standard Library
18. Concurrency
19. References
20. Revision History
21. Conditions of Use

14. Templates

14.1 Template Declarations


14.1.1 Use variadic templates rather than an ellipsis

Use of the ellipsis notation … to indicate an unspecified number of arguments should be avoided. Variadic templates offer a type-safe alternative.

#include <iostream>
               
int print ()
{
std::cout << std::endl;
}
                
// @@+ Compliant: variadic template function [email protected]@
template
void print (First const & v, Rest const & ... args)
{
std::cout << v;
print (args ...); // recursive template instantiation or a call to print ()
}
               
void foo (int32_t i)
{
print ("And only ", i, " little ducks came back.");
}

If use of a variadic template is not possible, function overloading or function call chaining (e.g. similar to stream output) should be considered.

References

  • HIC++ v3.3 - 11.6
  • JSF AV C++ Rev C - 108
  • MISRA C++:2008 - 8-4-1

View references >

14.2 Template instantiation and specialization


14.2.1 Declare template specializations in the same file as the primary template they specialize

Partial and explicit specializations of function and class templates should be declared with the primary template. This will ensure that implicit specializations will only occur when there is no explicit declaration available.

// file.h
template<typename T>
void foo (T & t)
{
++t;
}
   
// file1.cpp
#include "file.h"
   
void bar1 ()
{
int32_t i = 3;
foo<int32_t> (i);
// primary template used, i is now 4
}
   
// file2.cpp
#include "file.h"
   
// @@- Non-Compliant [email protected]@
template<>
void foo<int32_t> (int32_t & t)
{
--t;
}

void bar2 ()
{
int32_t i = 3;
foo<int32_t> (i);
// explicit specialization used, i is now 2
}  

References

  • JSF AV C++ Rev C – 104
  • MISRA C++:2008 – 14-7-3

View references >

14.2.2 Do not explicitly specialize a function template that is overloaded with other templates

Overload resolution does not take into account explicit specializations of function templates. Only after overload resolution has chosen a function template will any explicit specializations be considered.

#include <cstdint>
   
template<typename T>
void f1 (T);                  // #1
   
template<typename T>
void f1 (T*);                 // #2
   
template<>
void f1<int32_t*> (int32_t*); // #3 // @@- Non-Compliant (Explicit specialization of #1) [email protected]@
   
void f2 (int32_t * p)
{
f1(p);                      // Calls #2
f1<int32_t*>(p);            // Calls #3
}

References

  • MISRA C++:2008 – 14-8-1

View references >

14.2.3 Declare extern an explicitly instantiated template

Declaring the template with extern will disable implicit instantiation of the template when it is used in other translation units, saving time and reducing compile time dependencies.

#include <cstdint>
               
// t.h
template <typename T>
class A1 { };
                 
template <typename T>
class A2 { };
                 
extern template class A2<int32_t>;
                 
// t.cpp
#include "t.h"
template class A1<int32_t>;         // @@- Non-Compliant [email protected]@
template class A2<int32_t>;         // @@+ Compliant [email protected]@

 

 

Request PDF Version

 

 

Book traversal links for 14. Templates

  • ‹ 13. Overloading
  • High Integrity C++ Coding Standard
  • 15. Exception Handling ›

Footer menu

  • Products
    • Version Control System
    • Helix Core
    • Methodics IPLM
    • VersIC
    • Enterprise Agile Planning
    • Hansoft
    • Dev Collaboration
    • Helix TeamHub
    • Helix Swarm
    • Helix4Git
    • Development Lifecycle Management
    • Helix ALM
    • Surround SCM
    • Static Analysis
    • Helix QAC
    • Klocwork
    • Development Tools & Libraries
    • HostAccess
    • HydraExpress
    • PV-WAVE
    • SourcePro
    • Stingray
    • Visualization
  • Solutions
    • By need
    • Version Control
    • Application Lifecycle Management
    • Agile Project Management
    • Backlog Management
    • Project Portfolio Management
    • Audit & Compliance
    • DevOps
    • Static Analysis
    • IP Lifecycle Management
    • By industry
    • Game Development
    • Life Sciences
    • Software
    • Automotive
    • Embedded Systems
    • Government
    • Finance
    • Energy & Utilities
    • Aerospace & Defense
    • Virtual Production
    • Semiconductor
  • Resources
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
  • Support
    • Documentation
    • Request Support
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download
  • Customers
    • Case Studies
  • About
    • Our Team
    • Our Culture
    • Careers
    • Press
    • Contact
  • Partners
    • Integrations
    • Resellers
  • Quick links
    • Try For Free
    • Helix Core Demo
    • Helix ALM Demo
    • Subscription Center
    • Customer Support Login
    • Licensing Requests
    • Educational Licenses
    • How to Buy
Home
Perforce

Copyright © 2021 Perforce Software, Inc. All rights reserved.  |  Sitemap  |  Terms of Use  |  Privacy Policy

Social menu

  • Facebook
  • Twitter
  • Google+
  • LinkedIn
  • YouTube
  • RSS