【发布时间】:2011-05-09 16:02:04
【问题描述】:
对不起,这个有趣的标题。
在 C++0x 之前,restrictions 使用函数局部结构(“局部类型”)作为模板参数。我的问题本质上是类似的限制是否适用于匿名结构。具体来说,在 trait 类的上下文中:
template <typename T>
struct trait;
template <>
struct trait<int> {
typedef int type;
};
template <typename T>
struct trait<std::basic_string<T> > {
typedef struct {
T value;
} type;
};
trait<std::string>::type foo; // Is this valid?
template <typename T>
void f() { }
f<trait<std::string>::type> >(); // Is this?
template <typename T>
void g() { f<typename trait<T>::type>(); }
g<std::string>(); // And this?
这是有效和可靠的吗?它可以在最新版本的 GCC 和 LLVM 中编译,但我仍然不确定这是否严格有效,以及它是否被 VC++ 和 ICC 理解。
【问题讨论】:
-
由于所讨论的类型不用作模板参数,我不确定这与本地类型的限制有何关系。
-
@Luc 我将添加一个示例。但问题本身就存在。以上有效吗?如果是这样,它对模板参数有效吗?
-
现在我想知道如果您创建一个匿名结构的实例并将其用作参数会发生什么。 (您可以使用
decltype获取其类型) -
f<trait<std::string>::type> >();肯定是无效的:D。 (还有一个额外的>) -
参见open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#62(当然,项目符号列表在 C++0x 中完全消失了。但请注意他们在那段时间添加的条目,以便清楚地了解用于链接目的的名称)。跨度>
标签: c++ templates c++11 anonymous-class