mpark / variant

C++17 `std::variant` for C++11/14/17
https://mpark.github.io/variant
Boost Software License 1.0
659 stars 88 forks source link

Compilation failed in Visual Studio 2017 #55

Closed zongsheng-zhao closed 5 years ago

zongsheng-zhao commented 5 years ago

Visual Studio version 15.8.3

tested code:

#include "mpark_variant.h"
int main()
{
    mpark::variant<int, float> obj;
}

Error message (sorry, it's in Chinese):

1>------ 已启动生成: 项目: playground, 配置: Debug Win32 ------
1>main.cpp
1>c:\repos\playground\playground\mpark_variant.h(935): error C2131: 表达式的计算结果不是常数
1>c:\repos\playground\playground\mpark_variant.h(935): note: 因读取超过生命周期的变量而失败
1>c:\repos\playground\playground\mpark_variant.h(935): note: 请参见“<traits_0>”的用法
1>c:\repos\playground\playground\mpark_variant.h(1757): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::traits<int,float>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(2059): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::impl<int,float>" 的引用
1>c:\repos\playground\playground\main.cpp(4): note: 参见对正在编译的 类 模板 实例化 "mpark::variant<int,float>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(963): error C2131: 表达式的计算结果不是常数
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 非常量参数或对非常量符号的引用导致了故障
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 请参见“$S3”的用法
1>c:\repos\playground\playground\mpark_variant.h(968): error C2131: 表达式的计算结果不是常数
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 非常量参数或对非常量符号的引用导致了故障
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 请参见“$S5”的用法
1>c:\repos\playground\playground\mpark_variant.h(973): error C2131: 表达式的计算结果不是常数
1>c:\repos\playground\playground\mpark_variant.h(973): note: 读取未初始化符号导致失败
1>c:\repos\playground\playground\mpark_variant.h(973): note: 请参见“copy_constructible_trait”的用法
1>c:\repos\playground\playground\mpark_variant.h(979): error C2131: 表达式的计算结果不是常数
1>c:\repos\playground\playground\mpark_variant.h(979): note: 读取未初始化符号导致失败
1>c:\repos\playground\playground\mpark_variant.h(979): note: 请参见“move_constructible_trait”的用法
1>c:\repos\playground\playground\mpark_variant.h(985): error C2131: 表达式的计算结果不是常数
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 非常量参数或对非常量符号的引用导致了故障
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\initializer_list(42): note: 请参见“$S7”的用法
1>c:\repos\playground\playground\mpark_variant.h(1757): error C2975: “unnamed-parameter”:“mpark::detail::copy_assignment”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1719): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1757): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1757): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1741): error C2975: “unnamed-parameter”:“mpark::detail::move_assignment”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1679): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1757): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::copy_assignment<mpark::detail::traits<int,float>,0>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(1741): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1741): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1594): error C2975: “unnamed-parameter”:“mpark::detail::copy_constructor”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1556): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1701): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::assignment<mpark::detail::traits<int,float>>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(1741): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::move_assignment<mpark::detail::traits<int,float>,0>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(1594): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1594): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1578): error C2975: “unnamed-parameter”:“mpark::detail::move_constructor”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1518): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1594): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::copy_constructor<Traits,0>" 的引用
1>        with
1>        [
1>            Traits=mpark::detail::traits<int,float>
1>        ]
1>c:\repos\playground\playground\mpark_variant.h(1578): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1578): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1471): error C2975: “unnamed-parameter”:“mpark::detail::destructor”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1423): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1540): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::constructor<mpark::detail::traits<int,float>>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(1578): note: 参见对正在编译的 类 模板 实例化 "mpark::detail::move_constructor<mpark::detail::traits<int,float>,0>" 的引用
1>c:\repos\playground\playground\mpark_variant.h(1471): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1471): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1472): error C2975: “unnamed-parameter”:“mpark::detail::destructor”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1423): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1472): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1472): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1595): error C2975: “unnamed-parameter”:“mpark::detail::copy_constructor”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1556): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1595): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1595): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>c:\repos\playground\playground\mpark_variant.h(1758): error C2975: “unnamed-parameter”:“mpark::detail::copy_assignment”的模板参数无效,应为编译时常量表达式
1>c:\repos\playground\playground\mpark_variant.h(1719): note: 参见“unnamed-parameter”的声明
1>c:\repos\playground\playground\mpark_variant.h(1758): error C2440: “specialization”: 无法从“int”转换为“mpark::detail::Trait”
1>c:\repos\playground\playground\mpark_variant.h(1758): note: 强制转换为枚举类型要求显式强制转换(static_cast、C 样式强制转换或函数样式强制转换)
1>已完成生成项目“playground.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
SylvainCorlay commented 5 years ago

As far as we are concerned, we have these issues with MSVC 2017 with most of our libraries that rely on constexpr. In my opinion, MSVC is not a viable option for C++ at the moment if you rely non-trivial template stuff.

mgieseki commented 5 years ago

This seems to be the same issue as in #48. It's already fixed in the master branch.

mpark commented 5 years ago

@mgieseki: Thanks!

Closed by #49.

mpark commented 5 years ago

Fixed the root cause in 657110d5ea11f344d29bce1d9609c7d76696cb64