小编Pet*_*dal的帖子

使用std :: enable_if和sizeof时未定义的类型.(视觉工作室2017)

如果具有默认模板参数的类型具有某些属性,那么当我编译时间测试时,我得到的似乎是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)

c++ sizeof visual-studio enable-if

5
推荐指数
0
解决办法
178
查看次数

标签 统计

c++ ×1

enable-if ×1

sizeof ×1

visual-studio ×1