我很难理解在以下情况下扣除是如何工作的:
template<class Category, Category code>
struct AImpl
{ };
template<class Category, Category code>
struct AHelper
{
using type = AImpl<Category, code>;
};
template<class Category, Category code>
using A = typename AHelper<Category, code>::type;
template<int code>
void doSomething(A<int, code> object)
{
}
以下是测试代码:
A<int, 5> a1;
doSomething(a1); // This does not compile
doSomething<5>(a1); // This compiles
为什么在这种情况下没有推导出a1?
如果您改为以以下方式修改A:
template<class Category, Category code>
struct A
{ };
两种都可以。有人知道为什么吗?
[编辑] 问题链接:混合别名和模板特化
AImpl
。这样,你就可以让客户端代码更轻松地定义对象,同时函数始终得到所需的内容。虽然不完美,但这就是 C++。 - StoryTeller - Unslander Monicanamespace yourLib {}
中,它就不会与其他代码冲突。只要yourLib
足够独特即可。 - StoryTeller - Unslander Monica