Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:LiteralType(since C++11)

      From cppreference.com
      <cpp‎ |named req
       
       
      C++ named requirements
       

      Specifies that a type is aliteral type. Literal types are the types ofconstexpr variables and they can be constructed, manipulated, and returned fromconstexpr functions.

      Note: the standard doesn't define a named requirement with this name. This is a type category defined by the core language. It is included here as a named requirement only for consistency.

      Contents

      [edit]Requirements

      A literal type is any of the following:

      • possibly cv-qualifiedvoid (so thatconstexpr functions can return void);
      (since C++14)
      • has atrivial(until C++20)constexpr(since C++20)destructor,
      • all of its non-static non-variant data members and base classes are of non-volatile literal types, and
      • is one of
      (since C++17)
      • has novariant members, or
      • has at least one variant member of non-volatile literal type,
      • has novariant members, or
      • has at least one variant member of non-volatile literal type,
      • a type with at least oneconstexpr (possibly template) constructor that is not a copy or move constructor.

      [edit]Notes

      A type can be literal even if all of its constexpr constructors are deleted, inaccessible, or cannot participate in overload resolution.

      struct A{constexpr A(int)= delete;char c;};// A is a literal typeconstexpr A v=std::bit_cast<A>('0');// OK in C++20// v has literal type and thus can be constexpr

      [edit]Example

      Literal type that extends string literals:

      Run this code
      #include <cstddef>#include <iostream>#include <stdexcept> class conststr// conststr is a literal type{constchar* p;std::size_t sz;public:template<std::size_t N>constexpr conststr(constchar(&a)[N]): p(a), sz(N-1){} constexprchar operator[](std::size_t n)const{return n< sz? p[n]:throwstd::out_of_range("");} constexprstd::size_t size()const{return sz;}}; constexprstd::size_t count_lower(conststr s){std::size_t c{};for(std::size_t n{}; n!= s.size();++n)if('a'<= s[n]&& s[n]<='z')++c;return c;} // An output function that requires a compile-time constant N, for testingtemplate<int N>struct constN{    constN(){std::cout<< N<<'\n';}}; int main(){std::cout<<"The number of lowercase letters in\"Hello, world!\" is ";    constN<count_lower("Hello, world!")>();// the string literal is implicitly// converted to conststr}

      Output:

      The number of lowercase letters in "Hello, world!" is 9

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1453C++11a literal class could have volatile data membersnot allowed
      CWG 1951C++11
      C++14
      it was unclear whether cv-qualifiedvoid (C++14)
      and class types (C++11) are literal types
      they are
      CWG 2096C++11for a union type to be literal, all its non-
      static data members must be literal
      only one non-static data
      member needs to be
      CWG 2598C++11for a union type to be literal, it must have
      at least one non-static data member
      it can have no non-
      static data member

      [edit]See also

      (C++11)(deprecated in C++17)(removed in C++20)
      checks if a type is a literal type
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/LiteralType&oldid=177799"

      [8]ページ先頭

      ©2009-2025 Movatter.jp