【发布时间】:2018-09-25 14:57:06
【问题描述】:
考虑以下代码:
#include <iostream>
struct FactoryTag
{
static struct Shape {} shape;
static struct Color {} color;
};
template <typename TFactory>
int factoryProducer(TFactory tag)
{
if constexpr (std::is_same<TFactory, FactoryTag::Shape>::value)
return 12;
else if constexpr (std::is_same<TFactory, FactoryTag::Color>::value)
return 1337;
}
int main()
{
std::cout << factoryProducer(FactoryTag::shape) << std::endl;
return 0;
}
它适用于g++ -std=c++1z Main.cpp,但在 Visual Studio 中设置了 MSVC 并支持 c++17,它提供了
Error LNK2001 unresolved external symbol "public: static struct FactoryTag::Shape FactoryTag::shape" (?shape@FactoryTag@@2UShape@1@A) StaticTest C:\Users\danielj\source\repos\StaticTest\StaticTest\StaticTest.obj 1
这是 MSVC 中的错误吗?
【问题讨论】:
-
您可以使用
static inline constexpr struct Shape {} shape{}; static inline constexpr struct Color {} color{};解决此问题,即为这些静态成员提供定义。 -
@VTT,你不需要
inline,因为constexpr他们已经内联了。
标签: c++ gcc visual-c++ c++17