I'm working on creating a template class where I have the following constraints:
type
of the class is generic
, it should have normal constructorstype
of the class is NOT generic
, it should have explicit constructors.The solution I came up with looks like this:
#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>;
This seemed to work just fine (I can instantiate the type with point2f point;
or corner2f corner;
, etc.), but some esoteric code that I wrote fails to compile in 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 in 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
What's strange is that GCC compiles this code with no problems.
I've also found that there are a few things that allow MSVC to compile:
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
};
The following does NOT compile in 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.
};
Is this a flaw in how I've written the constructors for my point2_base
class? Is there a different mechanism I should be using to conditionally set those constructors explicit
? Is this just a bug in MSVC?
This appears to be a bug in Visual Studio's compiler, which is fixed as of version 19.14. It is not clear to me which version of Visual Studio fixed this bug.