【发布时间】:2018-06-25 21:12:46
【问题描述】:
我正在尝试专门研究一个类型 s.t. 的类。它忽略了给定类型的常量。在这种情况下,类型是模板模板参数:
template <class T, typename Enable = void>
struct bar
{
bar()
{
static_assert(!std::is_same<T,T>::value, "no implementation of bar for type T");
}
};
template <template <typename> class FooType, class T>
struct bar<FooType<T>, typename std::enable_if<std::is_same<typename std::remove_cv<FooType<T>>::type, foo<T>>::value>::type>
{};
上述代码在 GCC 4.8.4 和 clang 5.0(使用 -std=c++11)中都抱怨当与匹配 FooType 模板参数化的类一起使用时 bar 未定义。即使我去掉了sfinae参数,还是找不到特化。
可以在此处找到此问题的示例:https://godbolt.org/g/Cjci9C。 在上面的示例中,特化的构造函数有一个静态断言,当与 const FooType 一起使用时,即使 sfinae 参数被硬编码为 void 也是如此。当与非 const FooType 一起使用时,一切都按预期工作。
有人可以解释一下为什么 constness 在这种情况下禁止类型推断(匹配?)。
编辑(更新代码):
这是一个完全可编译的 sn-p。我试图在这个 sn-p 中捕获最小的例子。原始链接已更新以反映此示例。
#include <assert.h>
#include <type_traits>
template <class T>
struct foo {};
template <class T, typename Enable = void>
struct bar
{
bar()
{
static_assert(!std::is_same<T,T>::value, "no implementation of bar for type T");
}
};
template <template <typename> class FooType, class T>
struct bar<FooType<T>, typename std::enable_if<std::is_same<typename std::remove_cv<FooType<T>>::type, foo<T>>::value>::type>
{};
int main()
{
bar<foo<int>> mut_foo; // This is fine.
// bar<const foo<int>> const_foo; // Error. No implementation found.
}
删除 main 第 2 行的注释会触发静态断言。我也试过 std::decay 和 std::remove_const 都没有成功。
编辑(非重复理由):
虽然链接的问题确实提出了类似的问题,但它不需要使用模板模板参数。它也只提供了一种解决问题的技术,并没有说明为什么给定的代码 sn-p 不起作用。有趣的是,该技术似乎不适用于模板模板参数,因为将以下 sn-p 替换为上述示例会导致相同的错误:
template <template <typename> class FooType, class T>
struct bar<FooType<T>,
typename std::enable_if<std::is_same<FooType<T>, foo<T>>::value || std::is_same<FooType<T>, const foo<T>>::value>::type>
{};
【问题讨论】: