我正在使用以下编译时“技巧”(基于 ADL)来创建一个仅由同一命名空间中的类有效/定义/调用的函数。
namespace Family1
{
struct ModelA{};
struct ModelB{};
template<typename T>
bool is_in_Family1(T const& t)
{
return true;
}
};
namespace Family2
{
struct ModelC{};
template<typename T>
bool is_in_Family2(T const& t)
{
return true;
}
};
Family1::ModelA mA;
Family2::ModelC mC;
is_in_Family1(mA); // VALID
is_in_Family1(mC); // ERROR
现在,我想使用这个原则(或类似的东西)来Foo::Bar
为属于每个命名空间的类(例如Family1
.
// I would like to specialize the method template Bar for classes in Family1
// namespace; and another specialization for classes in Family2 namespace
struct Foo
{
template<typename T>
void Bar( T& _T ){}
};
为了便于维护和每个命名空间中的大量类,如果可能的话,我想在不命名命名空间中的所有类的情况下执行此检查。