C++ 条件默认构造函数在GCC中编译,但不在MSVC中编译

C++ 条件默认构造函数在GCC中编译,但不在MSVC中编译,c++,gcc,visual-c++,template-meta-programming,explicit,C++,Gcc,Visual C++,Template Meta Programming,Explicit,我正在创建一个模板类,其中有以下约束: 如果类的类型是泛型,则它应该具有正常的构造函数 如果类的类型不是泛型,则它应该具有显式构造函数 我提出的解决方案如下所示: #include<type_traits> #include<vector> enum class point_type { generic, center, corner }; template<point_type type, typename T> struct point2_

我正在创建一个模板类,其中有以下约束:

  • 如果类的
    类型
    泛型
    ,则它应该具有正常的构造函数
  • 如果类的
    类型
    不是
    泛型
    ,则它应该具有显式构造函数
我提出的解决方案如下所示:

#include<type_traits>
#include<vector>

enum class point_type {
    generic, center, corner
};

template<point_type type, typename T>
struct point2_base {
    T x, y;

    template<point_type _type = type, typename std::enable_if_t<_type == point_type::generic, int> = 0>
    point2_base() :
        x(0), y(0) {
    }
    template<point_type _type = type, typename std::enable_if_t<_type != point_type::generic, int> = 0>
    explicit point2_base() :
        x(0), y(0) {
    }
};

using point2f = point2_base<point_type::generic, float>;
using corner2f = point2_base<point_type::corner, float>;
using center2f = point2_base<point_type::center, float>;
在MSVC中:

example.cpp
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/vector(927): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
33 : <source>(33): note: see reference to function template instantiation 'void std::vector<line,std::allocator<_Ty>>::emplace_back<>(void)' being compiled
        with
        [
            _Ty=line
        ]
33 : <source>(33): note: see reference to function template instantiation 'void std::vector<line,std::allocator<_Ty>>::emplace_back<>(void)' being compiled
        with
        [
            _Ty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(840): error C2783: 'point2_base<point_type::corner,float>::point2_base(void)': could not deduce template argument for '__formal'
13 : <source>(13): note: see declaration of 'point2_base<point_type::corner,float>::point2_base'
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(959): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,>(_Objty *)' being compiled
        with
        [
            _Ty=line,
            _Objty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(959): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,>(_Objty *)' being compiled
        with
        [
            _Ty=line,
            _Objty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(1097): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,>(std::allocator<_Ty> &,_Objty *)' being compiled
        with
        [
            _Alloc=std::allocator<line>,
            _Ty=line,
            _Objty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(1096): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,>(std::allocator<_Ty> &,_Objty *)' being compiled
        with
        [
            _Alloc=std::allocator<line>,
            _Ty=line,
            _Objty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/vector(928): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,>(_Ty *)' being compiled
        with
        [
            _Ty=line
        ]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/vector(928): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,>(_Ty *)' being compiled
        with
        [
            _Ty=line
        ]
33 : <source>(33): note: see reference to function template instantiation 'void std::vector<line,std::allocator<_Ty>>::emplace_back<>(void)' being compiled
        with
        [
            _Ty=line
        ]
33 : <source>(33): note: see reference to function template instantiation 'void std::vector<line,std::allocator<_Ty>>::emplace_back<>(void)' being compiled
        with
        [
            _Ty=line
        ]
Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.
Compiler exited with result code 2
example.cpp
/opt/编译器资源管理器/Windows /19.1025017/LIB/NET/IORIZ/Vector(927):警告C430:C++异常处理程序,但未启用展开语义。指定/EHsc
33:(33):注意:参见正在编译的函数模板实例化“void std::vector::emplace_back(void)”的参考
具有
[
_Ty=直线
]
33:(33):注意:参见正在编译的函数模板实例化“void std::vector::emplace_back(void)”的参考
具有
[
_Ty=直线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/xmemory0(840):错误C2783:'point2_base::point2_base(void)':无法推断'\u'的模板参数
13:(13):注意:参见“point2_base::point2_base”的声明
/opt/compiler explorer/windows/19.10.25017/lib/native/include/xmemory0(959):注意:请参阅正在编译的函数模板实例化“void std::allocator::construct(_Objty*)”
具有
[
_Ty=直线,
_对象=线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/xmemory0(959):注意:请参阅正在编译的函数模板实例化“void std::allocator::construct(_Objty*)”
具有
[
_Ty=直线,
_对象=线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/xmemory0(1097):注意:请参阅正在编译的函数模板实例化“void std::allocator_traits::construct(std::allocator&,_Objty*)”
具有
[
_Alloc=std::分配器,
_Ty=直线,
_对象=线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/xmemory0(1096):注意:请参阅正在编译的函数模板实例化“void std::allocator_traits::construct(std::allocator&,_Objty*)”
具有
[
_Alloc=std::分配器,
_Ty=直线,
_对象=线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/vector(928):注意:请参阅正在编译的函数模板实例化“void std::_Wrap_alloc::construct(_Ty*)”的参考
具有
[
_Ty=直线
]
/opt/compiler explorer/windows/19.10.25017/lib/native/include/vector(928):注意:请参阅正在编译的函数模板实例化“void std::_Wrap_alloc::construct(_Ty*)”的参考
具有
[
_Ty=直线
]
33:(33):注意:参见正在编译的函数模板实例化“void std::vector::emplace_back(void)”的参考
具有
[
_Ty=直线
]
33:(33):注意:参见正在编译的函数模板实例化“void std::vector::emplace_back(void)”的参考
具有
[
_Ty=直线
]
针对x64的Microsoft(R)C/C++优化编译器版本19.10.25017
版权所有(C)微软公司。版权所有。
编译器已退出,结果代码为2
奇怪的是,GCC编译这段代码时没有任何问题

我还发现有一些东西可以让MSVC编译:

struct line {
    point2f baseline;
    //If I remove the vector, the code compiles
};

//////////

struct line {
    point2f baseline;
    std::vector<int> characters; //The type of the vector doesn't seem to matter
    line() {} //Adding a trivial, explicitly declared constructor allows the code to compile
};
struct行{
点2f基线;
//如果我删除向量,代码就会编译
};
//////////
结构线{
点2f基线;
std::vector characters;//向量的类型似乎无关紧要
line(){}//添加一个简单的、显式声明的构造函数可以编译代码
};
以下内容不在MSVC中编译:

struct line {
    point2f baseline;
    std::vector<int> characters; //The type of the vector doesn't seem to matter
};

int main() {
    std::vector<line> lines;
    lines.emplace_back();
}
struct line {
    point2f baseline;
    std::vector<int> characters; //The type of the vector doesn't seem to matter
    line() = default; //Same errors as before.
};
struct行{
点2f基线;
std::vector characters;//向量的类型似乎无关紧要
line()=default;//错误与以前相同。
};

这是我为
point2\u base
类编写构造函数的方式中的一个缺陷吗?我是否应该使用不同的机制来有条件地设置那些构造函数
explicit
?这只是MSVC中的一个bug吗?

这似乎是Visual Studio编译器中的一个bug。我不清楚哪个版本的Visual Studio修复了此错误。

请尝试使用clang,看看这是否编译了您的代码。如果是这样,那么您可能在MSVC中发现了一个bug。@rubenvb-Clang编译它。您是否尝试过像诊断代码所说的那样使用/EHsc(在cl命令行上)编译它?至少没有E/C++对象在一个异常中不在范围内被破坏。@ SoRoelHayTeR删除了其中一个警告,但没有消除编译错误。