Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-6020

The "_t" and "_v" version of type traits should be used instead of "::type" and "::value"

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      When they were first introduced in the language, type traits, defined in header <type_traits>, required to use nested types (with ::type) or nested values (with ::value) to access the result of the trait. Since then, the language introduced templated alias declaration and variable templates that allow to define traits in a more direct and readable way.

      Even if the old variant still exists, the new one, which uses _t (C+14) and _v (C+17) suffixes as discriminant, should be preferred.

      Noncompliant Code Example

      template<class T>
      void f(T t) {
        static_assert (std::is_arithmetic<T>::value); // Noncompliant
        using rawType = std::remove_cv<T>::type; // Noncompliant
      }
      

      Compliant Solution

      template<class T>
      void f(T t) {
        static_assert (std::is_arithmetic_v<T>); // Compliant, C++17
        using rawType = std::remove_cv_t<T>; // Compliant, C++14
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              loic.joly Loïc Joly
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: