特征类的 typedef 中的匿名结构
抱歉这个有趣的标题。
在 C++0x 之前,使用函数本地结构(“本地类型”)作为模板存在限制论据。我的问题本质上是类似的限制是否适用于匿名结构。具体来说,在特征类的背景下:
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 是否可以理解它。
Sorry for the funny title.
Prior to C++0x, there are restrictions in the use of function-local structs (“local types”) as template arguments. My question is essentially if similar restrictions apply to anonymous structs. Specifically, in the context of a trait class:
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?
Is this valid and reliable? It compiles in recent versions of GCC and LLVM but I’m still insecure whether this is strictly valid, and whether it’s understood by VC++ and ICC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
作为参考,引用 14.3.1/2 中链接问题:
我的解释是 typedef struct 正在为未命名类型创建别名,因此它不能用作模板类型参数。进一步注意,在 C 中,
typedef struct {} Foo;
的处理方式与struct Foo {};
的处理方式相当不同,先例表明这两种形式并不等效(尽管不可否认,这种差异)没有出现在 C++ 中)。因此,您的第一个示例似乎有效(因为它没有使用未命名类型作为模板类型参数),而第二个和第三个示例在技术上是无效的(因为它们确实将其用作模板类型参数)。
最后,我不得不问,是否有理由不能命名该结构而不是对其进行 typedef 处理?
编辑:从 7.1.3/1 开始:
这强烈暗示以这种方式使用 typedef 不会引入适合用作模板类型参数的类型。
For reference, the quote from the linked question in 14.3.1/2:
My interpretation is that the
typedef struct
is creating an alias to an unnamed type and that it thus can't be used as a template type parameter. Further note that additionally in Ctypedef struct {} Foo;
is treated rather differently fromstruct Foo {};
giving precedent that the two forms are not equivalent (although admittedly that difference doesn't appear in C++).Thus it would appear your first example works (since it's not using the unnamed type as a template type parameter), while the second and third examples would be technically invalid (since they do use it as a template type parameter).
Finally in closing I have to ask, is there a reason you can't name the struct instead of
typedef
ing it?EDIT: From 7.1.3/1:
This strongly implies that using
typedef
in such a way does not introduce a type suitable for use as a template type-parameter.在即将发布的标准中,该语言的限制被取消。标准在
14.3.1 [temp.arg.type] /1
typedef 是一个有效的类型 ID。事实上,下一段包含这样一个示例:
14.3.1 [temp.arg.type] /2
(我已经修剪了大部分其他示例)该示例显示未命名类型可以用作类模板类模板和函数模板中的参数。
In the upcoming standard that restriction is removed from the language. The standard says in
14.3.1 [temp.arg.type] /1
And a typedef is a valid type-id. As a matter of fact the next paragraph contains such an example:
14.3.1 [temp.arg.type] /2
(Where I have trimmed most of the other examples) The example shows that an unnamed type can be used as a class template argument both in class templates and function templates.
定义匿名类和该类的 typedef 名称的
typedef
声明,typedef 名称是用于链接目的的类的名称。因此,如果该类满足其他条件,则使用该类作为模板参数是合法的。参见C++03标准的7.1.3p5
这是 C++0x FDIS 中的 7.1.3p9。
FWIW,此代码可以使用 MSVC2010 编译正常(模数拼写错误)。
A
typedef
declaration that defines an anonymous class and a typedef-name for that class, the typedef-name is the name of the class for linkage purposes. It is therefore legal to use that class as a template parameter if it meets the other criteria.See 7.1.3p5 of the C++03 standard
This is 7.1.3p9 in the C++0x FDIS.
FWIW, this code compiles OK with MSVC2010 (modulo typos).
嗯,这相当于
完全合法。
Well, that is equivalent to
which is completely legitimate.