你可以得到效果为您正在寻找通过以下(打印出0 1,顺便说一句):
#include <type_traits>
#include <iostream>
namespace detail
{
struct SZugBase{};
}
template <typename tTYPE>
struct SFoo
{
struct SZug : public detail::SZugBase {};
};
template<typename tType, bool IsFoo>
struct SBarBase
{
int value = 0;
};
template<typename tType>
struct SBarBase<tType, true>
{
int value = 1;
};
template <typename tTYPE>
struct SBar : public SBarBase<tTYPE, std::is_convertible<tTYPE, detail::SZugBase>::value>
{ /* stuff */ };
int main()
{
SBar<int> b0;
SBar<SFoo<int>::SZug> b1;
std::cout << b0.value << " " << b1.value << std::endl;
}
说明
首先,我们得到一个SZug
常规b类ASE:
namespace detail
{
struct SZugBase{};
}
template <typename tTYPE>
struct SFoo
{
struct SZug : public detail::SZugBase {};
};
注意以下几点:
SZugBase
不受任何参数,所以很容易独立地指的SFoo
SZugBase
参数来它是在一个detail
命名空间,所以,按照常见的C++约定,你告诉客户端你的代码忽略它。
现在我们给SBar
两个基类,专业上的东西是否可以转换为的SZug
非模板的基础:
template<typename tType, bool IsFoo>
struct SBarBase
{
int value = 0;
};
template<typename tType>
struct SBarBase<tType, true>
{
int value = 1;
};
最后,我们只需要做出SBar
这些基地的子类(取决于专业化):
template <typename tTYPE>
struct SBar : public SBarBase<tTYPE, std::is_convertible<tTYPE, detail::SZugBase>::value>
{ /* stuff */ };
请注意,你不擅长SBar
她e,你更专门化基类。不过,这实际上会产生相同的效果。
你打算做什么之后?我不清楚你期望“专业化”做什么。你能告诉我们你将如何声明一个非专业化和专业化的SBar变量吗? – Holt
我实际上将这些纯粹用作特征类型,所以它们从未实际实例化过。 Bar简单地为另一个(未提及的)类提供一个constexpr成员指针(以及其他一些东西)。通常情况下,成员指针需要明确指定,但对于“SZug”专业化可以确定。 - (最终这与从1到N通用容器生成N对N通用嵌入容器有关 - 成员指针指向用户的容器或节点信息“嵌入”(即成员变量)类型)。 – xaxazak
我可以稍微修改一下'SZug'来检测它,用SFINAE做你想做的事很容易(见我的答案)。如果你不能,我不确定是否有办法检测到'tTYPE'是'SFoo :: SZug'类。 –
Holt