- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
g++ 3.4.5 接受此代码:
template <typename T> struct A
{
static const char* const str;
};
struct B {};
typedef A<B> C;
template<> const char* const C::str = "B";
// Equivalent to following?
// template<> const char* const A<B>::str = "B";
但我不确定它是否真的合法 C++03。特别是,
[14.7p3] In an explicit specialization declaration for a class template, a member of a class template or a class member template, the name of the class that is explicitly specialized shall be a template-id.
这个要求是说在这个例子的最后必须使用非 typedef 版本吗?还是我误解了什么?
编辑:进一步证据:Defect Report 403建议将类型(在该上下文中,函数调用表达式的参数类型)说成 template-id 是不正确的,因为 template-id 有一个句法意义,而不是语义意义。标准的后续草案在 3.4.2 中使用了“类模板特化”而不是“template-id”。
这支持了这样的论点,尽管 A<B>
和 C
表示相同的类型(并且具有相同或几乎相同的语义),A<B>
是一个 template-id 和 C
不是,因为术语 template-id 将句法内容称为标记序列,而不是这些标记的含义。
最佳答案
我认为这是严格的病式(至少根据 C++ '03)。
我的逻辑是,虽然 typedef (7.1.3/1):
... is thus a synonym for another type.
标准仍然明确允许在需要 class-name
的地方使用 typedef (7.1.3/4):
A typedef-name that names a class is a class-name
template-id
没有这样的词。
关于c++ - 显式成员特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4464300/
我是一名优秀的程序员,十分优秀!