如何在 c++17 中模拟 std::is_constant_evaluated?
我有以下代码在 c++20 中执行我想要的操作:
#include <iostream>
struct IntContainer
{
int value;
constexpr IntContainer(int init):value(init)
{
if(std::is_constant_evaluated())
{
value*=2;
}
else
{
std::cout<<"Constructed at runtime"<<std::endl;
}
}
};
int main()
{
constexpr int fixed=99;
int runtime;
std::cout<<"Enter runtime int value"<<std::endl;
std::cin>>runtime;
constexpr IntContainer fixed_container(fixed);
IntContainer runtime_container(runtime);
return 0;
}
对于 fixed
整数值,它会默默地构造我的容器并将该值加倍,对于 runtime
整数值,它使用详细结构。该实现允许我将 fixed_container
声明为 constexpr
。
我必须使用 c++20 才能使用 std::is_constant_evaluated
功能,但我仅限于 c++17。是否有一些聪明的模板魔法我可以用来在没有此功能的情况下保持相同的行为?
I've got the following code doing what I want in c++20:
#include <iostream>
struct IntContainer
{
int value;
constexpr IntContainer(int init):value(init)
{
if(std::is_constant_evaluated())
{
value*=2;
}
else
{
std::cout<<"Constructed at runtime"<<std::endl;
}
}
};
int main()
{
constexpr int fixed=99;
int runtime;
std::cout<<"Enter runtime int value"<<std::endl;
std::cin>>runtime;
constexpr IntContainer fixed_container(fixed);
IntContainer runtime_container(runtime);
return 0;
}
For the fixed
integer value, it constructs my container silently and doubles the value, for the runtime
integer value, it uses the verbose construction. The implementation allows me to declare fixed_container
as constexpr
.
I had to use c++20 for this to use the std::is_constant_evaluated
feature, but I'm restricted to c++17. Is there some clever template magic I could employ to keep the same behavior without this feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用哪个编译器?虽然
std::is_constant_evaluated
仅自 C++20 起可用,但编译器通常具有类似的扩展函数来确定其库中更好的算法。例如,GCC 在
is_constant_evaluated
之前使用__builtin_constant_p
。演示:GCC 6.1 运行 C++17
Which compiler are you using? While
std::is_constant_evaluated
is only available since C++20, compilers often have extension function like that to determine better algorithms in their library.For instance, GCC uses
__builtin_constant_p
prior tois_constant_evaluated
.Demo: GCC 6.1 running C++17
我找到了一种与 C++11 标准兼容的 MSVC 编译器的方法:
https://godbolt.org/z/o8of9z6Gn
说明:
成员指针相等运算符在 Visual Studio 中的编译时和运行时工作方式不同。编译时相等性检查完全匹配(
&N::a
和&M::a
不同,因为 Base 不同),但仅运行时检查类的偏移量(&N::a
和&M::a
具有相同的偏移量:0)。int M::*
和int N::*
指针默认是不具有可比性的,但是如果我们创建一个具有相同偏移量的公共基类,并使用成员强制转换(C ++20 ISO:§ 7.3.12, 2 和 § 7.6.1.7, 12),它们在公共C
基类上具有可比性。I found a way with MSVC compiler compatible from C++11 standard:
https://godbolt.org/z/o8of9z6Gn
Explanation:
The member pointer equality operator works differently on compile time and runtime in visual studio. Compile time the equality checks the exact match (
&N::a
and&M::a
are different, because of a different Base), but runtime it only checks the offset of the class (&N::a
and&M::a
are at the same offset: 0).int M::*
andint N::*
pointers are not comparable default, but if we create a common base class with the same offset, and use member casts (C++20 ISO: § 7.3.12, 2 and § 7.6.1.7, 12), they will comparable at the commonC
base class.