【发布时间】:2016-05-05 19:45:17
【问题描述】:
这似乎是visual-studio 问题。此代码runs fine in gcc 但无法在 Visual Studio 中编译:
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;
true_type foo();
template <typename T>
struct bar{
using def = conditional_t<decltype(foo())::value, char, void>;
};
int main() {
cout << typeid(bar<int>::def).name() << endl;
cout << decltype(foo())::value << endl;
}
给出的错误是:
错误 C2146:语法错误:在标识符
value之前缺少>
是否有针对此问题的错误修复或解决方法?
【问题讨论】:
-
你使用什么编译器? (似乎在gcc 6.1 上工作)
-
@milleniumbug 不幸的是我在 Visual Studio 上似乎有一个错误:(
-
以下是一些关于直接将作用域运算符与 decltype 一起使用的老问题:decltype and the scope operator in C++ 和 Why does scope resolution fail in presence of decltype?。好像现在有效,但是微软还不支持。
-
@ChristopherOicles 是的,我将其报告为错误。似乎它实际上正在使
struct触发它的template,不幸的是我需要模板。 -
我不确定它是否能帮助你,但我可以像这样解决这个问题:首先定义:
template <typename I> using id = I;然后用id<decltype(foo())>::value替换decltype(foo())::value的每个实例。奇怪的是cout这行好像可以编译,但是using def = ...却触发了错误。
标签: visual-studio c++ conditional metaprogramming value-type decltype