c++ - MSVC++ : template's static_assert is not triggered inside a lambda

标签 c++ visual-c++ c++17

更新 2:

这已在 VS 2019 Preview 16.1 Preview 1 中得到修复。

更新:

我已在 visualstudio.com 提交错误报告.


所以我开始研究 C++ 的模板,当我试图阻止使用 static_assert 编译模板类时遇到了这个问题。

基本上,static_assert 错误在 VS2017 上使用 C++ 语言标准:ISO C++17 标准 (/std:c++17)

我也在 gcc-7 上使用 -std=c++17 进行了尝试,并触发了错误。这是 VS2017 上的错误还是我遗漏了什么?

代码示例:

#include <iostream>
#include <string>
#include <type_traits>


template<typename T, typename Enable = void>
class IntegralContainer
{
    static_assert(std::is_integral<T>::value, "Type must be an integral!");
};

template<typename T>
class IntegralContainer<T, typename std::enable_if< std::is_integral<T>::value >::type >
{
private:
    T _value;

public:
    IntegralContainer(T value)
        : _value(value)
    {
    }
};

int main()
{
    IntegralContainer<int> int_container(1);
    // static_assert message is shown here.
    // > error C2338: Type must be an integral!
    // IntegralContainer<std::string> str_container;

    []() {
        // static_assert is not triggered here.
        IntegralContainer<std::string> str_container;
    }();

    std::cout << "Hello World!\n";

    return 0;
}

最佳答案

它正在优化永远无法执行的代码。 绝对是一个错误。 lambda 正在进行一些(可能是语义替换)基本优化。

生成的程序集显示没有为该行生成代码 IntegralContainer str_container

  []() {
00F72280  push        ebp  
00F72281  mov         ebp,esp  
00F72283  sub         esp,0D8h  
00F72289  push        ebx  
00F7228A  push        esi  
00F7228B  push        edi  
00F7228C  push        ecx  
00F7228D  lea         edi,[ebp-0D8h]  
00F72293  mov         ecx,36h  
00F72298  mov         eax,0CCCCCCCCh  
00F7229D  rep stos    dword ptr es:[edi]  
00F7229F  pop         ecx  
00F722A0  mov         dword ptr [this],ecx  
    // static_assert is not triggered here.
    IntegralContainer<std::string> str_container;
  }();
00F722A3  push        edx  
00F722A4  mov         ecx,ebp  
00F722A6  push        eax  
00F722A7  lea         edx,ds:[0F722BCh]  
00F722AD  call        @_RTC_CheckStackVars@8 (0F712B2h)  
00F722B2  pop         eax  
00F722B3  pop         edx  
00F722B4  pop         edi  
00F722B5  pop         esi  
00F722B6  pop         ebx  
00F722B7  mov         esp,ebp  
00F722B9  pop         ebp  
00F722BA  ret  

如果静态断言放在公共(public) ctor 中。

#include <iostream>
#include <string>
#include <type_traits>


template<typename T, typename Enable = void>
class IntegralContainer
{
public:
  IntegralContainer(T const& value) {
    static_assert(std::is_integral<T>::value, "Type must be an integral!");
  }
};

template<typename T>
class IntegralContainer<T, typename std::enable_if< std::is_integral<T>::value >::type >
{
private:
  T _value;

public:
  IntegralContainer(T value)
    : _value(value)
  {
  }
};

int main()
{
  IntegralContainer<int> int_container(1);
  // static_assert message is shown here.
  // > error C2338: Type must be an integral!
  //IntegralContainer<std::string> str_container;

  []() {
    // static_assert is not triggered here.
    IntegralContainer<std::string> str_container(std::string(""));
  }();

  std::cout << "Hello World!\n";

  return 0;
}

static_assert 被触发。

关于c++ - MSVC++ : template's static_assert is not triggered inside a lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441690/

相关文章:

c++ - 为什么我们应该在下面的代码中使用模板包装器?

c++ - 基于交换的排序算法的交换次数的奇偶性

c++ - 使用全局对象作为 COM 类工厂是否合法?

c++ - Visual Studio 2015 中的 _ITERATOR_DEBUG_LEVEL 0 导致字符串流访问冲突

c++ - vc++中是否存在编译器错误? (我是初学者)

c++ - 如何将当前替代类型的 std::variant 传递给可调用对象?

c++ - `unordered_map`中的高效节点extract()+insert()

c++ - 派生类 : using Base class member in initializer list

templates - 可变参数模板、std::function 和 lambda 作为类成员

c++ - 这是一个错误吗? constexpr 构造函数静默变为非 constexpr