Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::integral_constant<T, v>::value always have a definition?

Tags:

c++

c++14

In the C++14 standard, the std::integral_constant template is defined as follows:

template <class T, T v>
struct integral_constant {
  static constexpr T value = v;
  typedef T value_type;
  typedef integral_constant<T,v> type;
  constexpr operator value_type() const noexcept { return value; }
  constexpr value_type operator()() const noexcept { return value; }
};

It doesn't say whether there's a corresponding out-of-line definition for the static data member, i.e.,

template <class T, T v>
constexpr T integral_constant<T, v>::value;

I looked through the standard for a requirement that such a definition be provided, but couldn't find one, so I don't know whether portable code may odr-use value.

(In C++17, static constexpr members became implicitly inline, making the in-class declaration a definition.)

like image 952
Brian Bi Avatar asked Apr 02 '19 15:04

Brian Bi


1 Answers

This is covered by a blanket declaration in [contents]/1 that defines the general rules for standard library implementations:

The C++ standard library provides definitions for the following types of entities: macros, values, types, templates, classes, functions, objects.

Variables of non-reference types are objects, so value here is an object. Therefore, the standard library must provide a definition for it.

The C++17 version of this statement is more direct on this matter:

The C++ standard library provides definitions for the entities and macros described in the synopses of the C++ standard library headers.

value is most assuredly an entity, which is described in the synopsis of a C++ standard library header. Therefore, a definition must be provided.

like image 73
Nicol Bolas Avatar answered Sep 19 '22 03:09

Nicol Bolas