c++ - 条件默认构造函数可以在 GCC 中编译,但不能在 MSVC 中编译

标签 c++ gcc visual-c++ template-meta-programming explicit

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

  • 如果类的typegeneric,它应该有普通的构造函数
  • 如果类的类型不是泛型,则它应该具有显式构造函数。

我想出的解决方案如下所示:

#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>;

这似乎工作得很好(我可以用 point2f point;corner2fcorner; 等实例化类型),但是我编写的一些深奥代码失败了在 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();
}

Godbolt reports these errors在 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

奇怪的是,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
};

以下内容无法在 MSVC 中编译:

struct line {
    point2f baseline;
    std::vector<int> characters; //The type of the vector doesn't seem to matter
    line() = default; //Same errors as before.
};

这是我为 point2_base 类编写构造函数的方式中的缺陷吗?我应该使用不同的机制来有条件地设置这些构造函数显式吗?这只是 MSVC 中的一个错误吗?

最佳答案

这似乎是 Visual Studio 编译器中的一个错误,which is fixed as of version 19.14 。我不清楚哪个版本的 Visual Studio 修复了这个错误。

关于c++ - 条件默认构造函数可以在 GCC 中编译,但不能在 MSVC 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47370534/

相关文章:

c++ - Tcl 脚本非检测调试器使用 Tcl 库和/或 Tcl 内部?

c++ - gcc 的扩展初始化程序列出了警告

GCC 预处理器删除注释

c++ - 为什么一个int8_t的输出格式要用4个字节?

c++ - 函数后的 const 如何优化程序?

c - -lm 不在 makefile 中链接数学库

c# - C#或python用于调用C++ DLL进行硬件控制

c++ - CTime初始化

c++ - C4244 : '+=' : conversion from 'std::streamsize' to 'size_t' , 可能丢失数据

c++ - CEDel.exe : 0xC0000005: Access violation writing location 0x002e0364 中 0x03f7111c 的第一次机会异常