繁体   English   中英

MSVC ++:模板的static_assert不在lambda内触发

[英]MSVC++: template's static_assert is not triggered inside a lambda

更新2:

现在修复了VS 2019预览版16.1预览版1。

更新:

我在visualstudio.com上提交了一份错误报告。


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

基本上,当使用C ++语言标准在ISO20 ++ 17标准版(/ std:c ++ 17)中,在VS2017上的lambda内部时,不会触发static_assert错误。

我也使用-std = c ++ 17在gcc-7上尝试了这个并且触发了错误。 这是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;
}

它正在优化远离永远无法执行的代码。 肯定是一个bug。 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  

如果静态断言放在公共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被触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM