Clang 编译器为什么无法编译以下variadic模板代码
Why does Clang++ compiler fail to compile the following variadic template code?
#include <string>
#include <iostream>
#include <tuple>
#include <utility>
template<typename... T> struct test {
using args_type = std::tuple<T...>;
args_type x;
template<std::size_t... I>
void callme(std::index_sequence<I...>) {
int _[] = {(std::get<I>(x).std::tuple_element<I, args_type>::type::~type(), true)...};
}
};
int main() {
}
错误消息是
clang-3.7 -std=gnu++1y -Wc++14-extensions test.cpp
test.cpp:15:56: error: expected ')'
int _[] = {(std::get<I>(x).std::tuple_element<I, args_type>::type::~type(), true)...};
^
test.cpp:15:20: note: to match this '('
int _[] = {(std::get<I>(x).std::tuple_element<I, args_type>::type::~type(), true)...};
^
1 error generated.
与G 4.9.2相同的代码似乎可以弥补。我还找不到任何相关的错误报告。
似乎是一个叮当虫,尽管这种伪毁灭者名称的查找可能是违反的,并且对开放CWG问题的主题,特别是555和399。
。膨胀模式的重要位是
std::get<I>(x).std::tuple_element<I, args_type>::type::~type()
在这里, .
和 ()
之间的位是 pseudo-destructor-name ;然后,合格的名称查找要求
如果A pseudo-Destructor-name (5.2.4)包含a nested-name-specifier , type-name s被查找为类型 Nested-Name-Pesifier 。同样,在表格的A 合格-ID中:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;class-name
:: ~
class-name第二个 class-name 在与第一个的范围相同的范围中查找。
即。type
在std::tuple_element<I, args_type>
中查找,发现它指某种类型。请注意, class-name 是标识符(和 simple-template-id s)的语法名称,无需参考实际类。std::get<I>(x).std::tuple_element<I, args_type>::type::~type
然后是指type
的破坏者。
具有辅助功能的解决方法:
template <typename T>
void destroy(T& p) {p.~T();}
template<typename... T> struct test {
using args_type = std::tuple<T...>;
args_type x;
template<std::size_t... I>
void callme(std::index_sequence<I...>) {
int _[] = {(destroy(std::get<I>(x)), 0)...};
}
};
有趣的是,来自IBM的HSTONG提到了一些可以更好地工作的解决方法。
int _[] = {(std::get<I>(x).::std::tuple_element<I, args_type>::type::~type(), true)...};
或
int _[] = {(std::get<I>(x).std::template tuple_element<I, args_type>::type::~type(), true)...};
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- variadic模板代码中的GCC VS MSVC编译误差
- Clang 编译器为什么无法编译以下variadic模板代码