“如果...则为constexpr".对比"if"进行优化-为什么"constexpr"需要吗? [英] "constexpr if" vs "if" with optimizations - why is "constexpr" needed?

查看:164
本文介绍了“如果...则为constexpr".对比"if"进行优化-为什么"constexpr"需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 1z将引入"constexpr if"-根据条件将if删除分支之一.似乎合理且有用.

C++1z will introduce "constexpr if" - an if that will have one of branches removed, based on the condition. Seems reasonable and useful.

但是,没有constexpr关键字是不可能的吗?我认为在编译期间,编译器应该知道在编译期间是否知道条件.如果是这样,即使是最基本的优化级别也应删除不必要的分支.

However, is it not possible to do without constexpr keyword? I think that during compilation, compiler should know wheter condition is known during compilation time or not. If it is, even the most basic optimization level should remove unnecessary branch.

例如(请参见godbolt: https://godbolt.org/g/IpY5y5 ):

For example (see in godbolt: https://godbolt.org/g/IpY5y5):

int test() {
    const bool condition = true;
    if (condition) {
      return 0;
    } else {
      // optimized out even without "constexpr if"
      return 1;
    }
}

Godbolt Explorer显示,即使带有-O0的gcc-4.4.7也不编译返回1",因此它实现了constexpr if所承诺的功能.显然,当条件是constexpr函数的结果时,这样的旧编译器将无法执行此操作,但事实仍然存在:现代编译器知道条件是否为constexpr,不需要我明确地告诉它.

Godbolt explorer shows, that even gcc-4.4.7 with -O0 did not compile "return 1", so it achieved what was promised with constexpr if. Obviously such old compiler will not be able to do so when condition is result of constexpr function, but fact remains: modern compiler knows whether condition is constexpr or not and doesn't need me to tell it explicitly.

所以问题是:

为什么在"constexpr if"中需要"constexpr"?

Why is "constexpr" needed in "constexpr if"?

推荐答案

通过示例很容易解释.考虑

This is easy to explain through an example. Consider

struct Cat { void meow() { } };
struct Dog { void bark() { } };

template <typename T>
void pet(T x)
{
    if(std::is_same<T, Cat>{}){ x.meow(); }
    else if(std::is_same<T, Dog>{}){ x.bark(); }
}

调用

pet(Cat{});
pet(Dog{});

将触发编译错误 ( wandbox示例) ,因为if语句的两个分支都必须格式正确.

will trigger a compilation error (wandbox example), because both branches of the if statement have to be well-formed.

prog.cc:10:40: error: no member named 'bark' in 'Cat'
    else if(std::is_same<T, Dog>{}){ x.bark(); }
                                     ~ ^
prog.cc:15:5: note: in instantiation of function template specialization 'pet<Cat>' requested here
    pet(Cat{});
    ^
prog.cc:9:35: error: no member named 'meow' in 'Dog'
    if(std::is_same<T, Cat>{}){ x.meow(); }
                                ~ ^
prog.cc:16:5: note: in instantiation of function template specialization 'pet<Dog>' requested here
    pet(Dog{});
    ^


更改pet以使用if constexpr


Changing pet to use if constexpr

template <typename T>
void pet(T x)
{
    if constexpr(std::is_same<T, Cat>{}){ x.meow(); }
    else if constexpr(std::is_same<T, Dog>{}){ x.bark(); }
}

仅要求分支可解析-仅匹配条件的分支需要格式正确的 ( wandbox示例) .

only requires the branches to be parseable - only the branch that matches the condition needs to be well-formed (wandbox example).

摘要

pet(Cat{});
pet(Dog{});

将按预期进行编译和工作.

will compile and work as expected.

这篇关于“如果...则为constexpr".对比"if"进行优化-为什么"constexpr"需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆