如何在 c++17 中模拟 std::is_constant_evaluated?

发布于 2025-01-16 16:33:06 字数 913 浏览 2 评论 0原文

我有以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2025-01-23 16:33:06

您使用哪个编译器?虽然 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 to is_constant_evaluated.

Demo: GCC 6.1 running C++17

轮廓§ 2025-01-23 16:33:06

我找到了一种与 C++11 标准兼容的 MSVC 编译器的方法:

constexpr bool is_constant_evaluated() noexcept {
    struct C {};
    struct M : C { int a; };
    struct N : C { int a; };

    return &M::a != static_cast<int C::*>(&N::a);
}

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:

constexpr bool is_constant_evaluated() noexcept {
    struct C {};
    struct M : C { int a; };
    struct N : C { int a; };

    return &M::a != static_cast<int C::*>(&N::a);
}

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::* and int 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 common C base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文