如果具有默认模板参数的类型具有某些属性,那么当我编译时间测试时,我得到的似乎是Visual Studio错误.在下面的最小例子中我使用std::is_integer
.
使用Visual Studio 2017编译以下程序时
#include <type_traits>
#include <utility>
#include <algorithm>
template<typename U,
typename Enabled = typename std::enable_if<std::is_integral<U>::value>::type>
struct wrapper
{
};
template<typename U, typename storage_type>
using is_small = std::is_void<typename std::enable_if <
(sizeof(wrapper<U, char>) <= sizeof(storage_type))
>::type>;
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
1>bug.cpp(13): error C2027: use of undefined type 'wrapper<U,char>'
1>bug.cpp(13): note: see declaration of 'wrapper<U,char>'
Run Code Online (Sandbox Code Playgroud)
相同的程序在g ++ 6.1 上编译.
Enable
删除默认参数时,程序会在Visual Studio上编译.另外,当我sizeof(...)<=sizeof(...)
在以下模板成员函数中执行相同的测试时,程序编译正常(is_small
删除).
struct c
{
template<typename U, typename storage_type>
typename std::enable_if<(sizeof(wrapper<U, char>) <= sizeof(storage_type))>::value
foo(U u, …
Run Code Online (Sandbox Code Playgroud)