是否可以分别专注于 typedef 及其基础类型?
Is it possible to specialize on a typedef and its underlying type separately?
简单的例子是size_t。假设:
typedef uint64_t size_t;
而且我想专注于 uint64_t 和 size_t:
template <class T>
struct MyType {};
template <>
struct MyType<uint64_t>{
void operation()() { cout << "uint64_t" << endl; }
};
template <>
struct MyType<size_t>{
void operation()() { cout << "size_t" << endl; }
}
MyType<1ULL>()();
MyType<static_cast<size_t>(1ULL)>()();
这可能吗?是否有获得相同净效果的技巧?
不可能。
标准(7.1.3,[dcl.typedef])内容如下:
A name declared with the typedef
specifier becomes a typedef-name. Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.
简单的例子是size_t。假设:
typedef uint64_t size_t;
而且我想专注于 uint64_t 和 size_t:
template <class T>
struct MyType {};
template <>
struct MyType<uint64_t>{
void operation()() { cout << "uint64_t" << endl; }
};
template <>
struct MyType<size_t>{
void operation()() { cout << "size_t" << endl; }
}
MyType<1ULL>()();
MyType<static_cast<size_t>(1ULL)>()();
这可能吗?是否有获得相同净效果的技巧?
不可能。
标准(7.1.3,[dcl.typedef])内容如下:
A name declared with the
typedef
specifier becomes a typedef-name. Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.