constexp 函数中的 C++ Wconversion 警告,但模板中没有

C++ Wconversion warning in constexp functions but not in templates

我的问题是为什么下面的代码会为 constexpr 函数而不是模板生成警告

我确实理解警告的含义以及如何摆脱它们。我不明白的是为什么编译器不为下面的 struct Testconstexpr 成员 ToDoubleToSquare 发出警告?

#include <iostream>

template <typename T, T value>
struct Test {
  static constexpr double ToDouble = value;

  static constexpr T ToSquare = value * value;
};

template <typename T>
constexpr double ToDouble(T value) {
  return value;
}

template <typename T>
constexpr T ToSquare(T value) {
  return value * value;
}

int main() {
  std::cout << Test<long, 1>::ToDouble << std::endl;
  std::cout << ToDouble(static_cast<long>(1)) << std::endl;

  std::cout << Test<char, 1>::ToSquare << std::endl;
  std::cout << ToSquare(static_cast<char>(1)) << std::endl; 
}
$ g++ -Wconversion -std=c++11 a.cc 
a.cc: In instantiation of ‘constexpr double ToDouble(T) [with T = long int]’:
a.cc:22:45:   required from here
a.cc:12:10: warning: conversion to ‘double’ from ‘long int’ may alter its value [-Wconversion]
   return value;
      ^~~~~
a.cc: In instantiation of ‘constexpr T ToSquare(T) [with T = char]’:
a.cc:25:45:   required from here
a.cc:17:16: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
   return value * value;

模板生成类型。在该类型的每个实例中,值 value 是一个编译时间常量。它可以检查精度损失。

constexpr情况下,模板函数的生成函数没有这个属性。参数 value 不保证是编译时常量。虽然您仅在它是编译时常量的上下文中使用它,但警告不会检查每个调用上下文。

可能是你理解错了constexpr;在函数上它声明函数 可以 编译时评估,而不是它 必须 .