Update 2:
This is fixed now in VS 2019 Preview 16.1 Preview 1.
Update:
I have filed a bug report at visualstudio.com.
So I'm starting to get into C++'s templates and I ran into this issue when trying to prevent a template class from being compiled using static_assert
.
Basically, the static_assert
error is not triggered when it is inside a lambda on VS2017 using C++ Language Standard: ISO C++17 Standard (/std:c++17).
I also tried this on gcc-7 using -std=c++17 and the error is triggered. Is this a bug on VS2017 or is there something I'm missing?
Code sample:
#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;
}
It's optimising away code which can never be executed. Definitely a bug. The lambda is undergoing some (probably semantic substitution) basic optimisation.
The resulting assembly shows no code being generated for the line 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
If the static assert is placed inside a public ctor though.
#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;
}
The static_assert is triggered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With