C++特征和专业化
我第一次尝试使用特征来使我能够在一些通用例程中简洁地关联相关类型。然而,我在语法上遇到了困难,谷歌搜索似乎没有找到任何实现此类事情的简单示例。这是我目前所拥有的简明示例:
template <typename T> struct foo_traits { };
template<> struct foo_traits<int> {
typedef unsigned char T2; // sub-type for int specialisation of foo_traits
};
template <typename T> T foo(void)
{
typedef foo_traits<T> traits_type; // OK
typedef traits_type::T2 T2; // error here: "Too few template-parameter-lists"
T i
T2 j;
// ...
}
What is the right way to get the T2
typedef from foo_traits
so that I can use it in my generic template function foo ?
Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?
I'm trying to use traits for the first time to enable me to succinctly associate related types in some generic routines. However I'm struggling with the syntax and Googling doesn't seem to turn up any simple examples for implementing this kind of thing. Here is condensed example of what I have currently:
template <typename T> struct foo_traits { };
template<> struct foo_traits<int> {
typedef unsigned char T2; // sub-type for int specialisation of foo_traits
};
template <typename T> T foo(void)
{
typedef foo_traits<T> traits_type; // OK
typedef traits_type::T2 T2; // error here: "Too few template-parameter-lists"
T i
T2 j;
// ...
}
What is the correct way to get the T2
typedef from foo_traits
so that I can use it in my generic template function foo
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奇怪的错误消息,但您确实必须告诉编译器嵌套的 T2 应该是类型名称
Odd error message, but you really have to tell the compiler that the nested T2 is supposed to be a type name