5

我正在使用以下编译时“技巧”(基于 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 ){}
    };

为了便于维护和每个命名空间中的大量类,如果可能的话,我想在不命名命名空间中的所有类的情况下执行此检查。

4

2 回答 2

1

你的“把戏”有一个大问题。尝试调用is_in_Family1(make_pair(Family1::ModelA(), Family2::ModelC()),您将看到 return true,因为 ADL 将同时查看 和 的命名空间ModelAModelC因为pair<ModelA, ModelC>)。

忽略这个问题,使用你的函数是直截了当的。

template<typename T> struct int_ { typedef int type; };

struct Foo
{
    template<typename T, 
             typename int_<decltype(is_in_Family1(*(T*)0))>::type = 0
    >
    void Bar( T& t ){}

    template<typename T, 
             typename int_<decltype(is_in_Family2(*(T*)0))>::type = 0
    >
    void Bar( T& t ){}
};

Bar取决于它是在family2还是family1中。

struct Foo
{
    template<typename T, 
             typename int_<decltype(is_in_Family1(*(T*)0))>::type = 0
    >
    void Bar( T& t, long){}

    template<typename T,
             typename int_<decltype(is_in_Family2(*(T*)0))>::type = 0
    >
    void Bar( T& t, long){}

    template<typename T>
    void Bar( T& t, int) {}

    template<typename T>
    void Bar( T& t ) { return Bar(t, 0); }
};

那一个也有一个通用的后备。而且您的代码具有未定义的行为,因为您使用了保留名称。不要使用_T.

于 2012-01-26T22:24:22.687 回答
0

我发现最快的方法是使用 Boost Type Traits' is_base_of<>

我尝试将继承与模板特化一起使用,但这不起作用,因为在使用模板特化时会忽略继承,因此您必须专门针对每个模型。多个类的父级的部分专业化的答案解释了这个问题。

如果您使 Family1::ModelA 和 Family::ModelB 的子类 Family1:Family1Type 和 Family2::ModelC 成为 Family2::Family2Type 的子类,则使用类型特征有效:

#include <iostream>
#include <boost/type_traits/is_base_of.hpp>

namespace Family1{

    struct Family1Type{};

    struct ModelA :public Family1Type{};
    struct ModelB :public Family1Type{};

    template<typename T>
    bool is_in_Family1(const T& t){
        return boost::is_base_of<Family1::Family1Type,T>::value;
    }
};

namespace Family2{
    struct Family2Type{};

    struct ModelC :public Family2Type{};

    template<typename T>
    bool is_in_Family2(const T& t){
        return boost::is_base_of<Family2::Family2Type,T>::value;
    }

};

using namespace std;
int main(int argc, char *argv[]) {

    Family1::ModelA mA;
    Family2::ModelC mC;

    std::cout << "mA is in Family1?  " << is_in_Family1(mA) << std::endl;
    std::cout << "mC is in Family2?  " << is_in_Family2(mC) << std::endl;

    //std::cout << "mC is in Family1?  " << is_in_Family1(mC) << std::endl; //ERROR!
    //std::cout << "mA is in Family2?  " << is_in_Family2(mA) << std::endl; //ERROR!

    return 0;
}

这将产生以下输出:

mA is in Family1?  1
mC is in Family2?  1

我认为没有一种方法可以根据不同命名空间中 'template<class _Tp> struct std::less' 的Foo专业化来声明和专门化Foo::Bar<>另一个命名空间

于 2012-01-25T20:37:45.433 回答