我觉得拥有一个“总是返回5的函数”会破坏或淡化“调用函数”的意义。C++11中肯定有某种原因或需求才会加入这个功能。为什么会有这个功能呢? // preprocessor. #define MEANING_OF_LIFE 42 // constants: const int Meaning...
我在使用constexpr时遇到了一些问题。书籍C++ Primer中展示了一行代码: constexpr int sz = size(); // only size() is a constexpr function // th...
考虑以下代码: static constexpr int make_const(const int i){ return i; } void t1(const int i) { constexpr int ii = make_const(i); // error occu...
最近我惊讶地发现,以下代码在clang、gcc和msvc也能编译通过(至少在它们当前的版本下)。 struct A { static const int value = 42; }; constexpr int f(A a) { return a.value; } void g(...
关键字 constexpr 在引入C++11标准时对函数实施了相当严格的限制。这些限制在C++14和C++20中得到了放松(最值得注意的是): C++14允许多个return语句,static_assert等。 C++20允许try和asm C++23进一步放宽了这些限制。从我在cpp...