帮助我解决“部分专业化中未使用的模板参数”问题
我一直在努力处理不是 C++0x 代码,而是常规 C++。不要问我为什么必须使用常规 C++,这只是某种愚蠢的要求。
所以事情是这样的:我需要在枚举中获取一个值为 1 或 0 的值,以判断某些陈述是对还是错。 当然,我模板化了一个枚举中包含 0 的结构,并使用第二个语句对其进行了专门化,其中枚举包含 1 而不是 0。
对我来说似乎很合法,但是,它告诉我应该使用专门化的参数。这有点奇怪,因为我尝试以所有可能的方式使用它,但它只是不断弹出此错误。
这是代码:
template<typename T>
struct CanPrint
{
template<size_t>
struct Value { enum { val = 0 }; };
template<size_t>
struct Value<sizeof(True)> { enum { val = 1 }; };
enum
{ value = Value<sizeof(IsTrue<T>(0))>::val };
};
我敢打赌,如果它不是部分专业化,那么它会起作用,但显式专业化不能在命名空间范围内。如果不专门化两个模板,我显然无法专门化模板中的模板。我可以吗?
I've been struggling with NOT C++0x code, but regular C++. Don't ask me why I have to use regular C++, it's just some kind of silly requirement.
So here's the thing : I need to get a value in an enum to be 1 or 0, regarding that some statement is true or false.
So of course, I templated a struct containing 0 in an enum, specializated it with the second statement where the enum contains 1 instead of 0.
Seems quite legit to me, however, it tells me that I should use the parameters of the specialization. Which is kinda strange, because I tried to use it in all possible ways and it just kept popping out this error.
Here's the code :
template<typename T>
struct CanPrint
{
template<size_t>
struct Value { enum { val = 0 }; };
template<size_t>
struct Value<sizeof(True)> { enum { val = 1 }; };
enum
{ value = Value<sizeof(IsTrue<T>(0))>::val };
};
I bet this would work if it wasn't a partial specialization, but explicit ones can't be at namespace-scope. And I obviously can't specialize a template within a template without specializing both of them. Can I ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,如果
True
不是模板参数,则它不是部分特化。是完全专业化。由于这是完全专业化,因此您无法在类内部(即类范围)定义它。完全专业化只能在命名空间范围内定义。因此,在命名空间范围内定义
Value
,主要的以及专门化的。或者,您也可以这样做:
查看在线演示:http://www.ideone.com/MSG5X
By the way, its not partial specialization if
True
isn't template argument. It is full specialization .And since this is full specialization, you cannot define it inside the class, i.e at class-scope. Full specialization can be defined only at namespace-scope. So define
Value
, primary well as the specialization, at namespace scope.Or, you can do this instead:
See the online demo : http://www.ideone.com/MSG5X
您仅列出部分专业化的参数:
不适用于完全专业化:
You only list parameters for partial specializations:
Not for full specializations: