MSVC使用constexpr if

时间:2018-07-13 10:27:27

标签: c++ visual-c++ variadic-templates if-constexpr

我有一个问题,我几乎可以肯定是MSVC错误,但也许我缺少了一些东西。

这是实际代码的简化版本:

template <typename... Args>
class InnerType {};

template <typename... Args>
class OuterType {
public:
    void foo1() {
        if constexpr (true) {
            bar(InnerType<Args...>());
        }
    }

    void foo2() {
        if (true) {
            bar(InnerType<Args...>());
        }
    }

    void bar(InnerType<Args...>) {}
};

如您所见,foo1()foo2()之间的唯一区别就是constexpr if。当我尝试在MSVC中编译一些测试时,会发生以下情况:

 OuterType<const bool> test1;
 test1.foo1(); // does not compile
 test1.foo2(); // compiles

 OuterType<bool> test2;
 test2.foo1(); // compiles
 test2.foo2(); // compiles

我为test1.foo1()遇到的错误是:

error C2664: 'void OuterType<const bool>::bar(InnerType<const bool>)':
cannot convert argument 1 from 'InnerType<bool>' to 'InnerType<const bool>'

在使用GCC的Linux上,相同的代码编译没有问题。此外,在非基本类型中尝试相同的操作在MSVC中同样适用:

 class SomeType {};

 OuterType<const SomeType> test;
 test.foo1(); // compiles
 test.foo2(); // compiles

因此,由于某些原因,使用constexpr if和const基本类型作为模板参数时,似乎const被吞噬了。如果它不是可变参数模板,则不会发生此问题。

Here是一个实时示例。

我是不是认为这是编译器错误?

1 个答案:

答案 0 :(得分:0)

So it turned out to be a bug.对于遇到相同问题的任何人,Microsoft表示他们将在VS 15.8 Preview 4中对其进行修复。