我有一个不幸的类设置,我被困住了,我需要调用一个专门针对类的每个变体的简单函数。
下面是显示问题的代码的简化版本。
https://godbolt.org/z/advGxdnPK
template <typename T>
struct ValueT {};
struct Container1 {
using Value = ValueT<Container1>;
};
struct Container2 {
using Value = ValueT<Container2>;
};
template <typename T>
struct Container3 {
using Base = T;
struct Value : ValueT<Container3> {
typename Base::Value asBase() const { return {}; }
};
};
struct Container4 {
using Value = ValueT<Container4>;
};
void handle(const Container1::Value &v) {}
void handle(const Container2::Value &v) {}
template<typename T>
void handle(const typename Container3<T>::Value &v) {
return handle(v.asBase());
}
// intentionally omitted
// void handle(const Container4::Value &v) {}
int main()
{
Container1::Value v1;
Container2::Value v2;
Container3<Container1>::Value v31;
Container3<Container2>::Value v32;
Container4::Value v4;
// These should compile.
handle(v1);
handle(v2);
handle(v31);
handle(v32);
// This one should fail to compile.
handle(v4);
return 0;
}
我尝试了几种方法来编译
handle(v31)
,而 handle(v4)
仍然无法编译。直至并包括 template template template parameters
!但是,我找不到合适的角度。
@user12002570 指出它不能仅使用函数签名,因为
const typename Container3<T>::Value
处于非推导上下文中。
我想我已经足够接近使用Doing a static_assert that a template type is another template
中的技术了