内部 typedef 的变化取决于父 class

Inner typedef changes depending on parent class

我放弃了,请帮忙解释一下这个行为。我在下面给出的示例是我能想到的最简单的示例,但它总结了问题(在启用了 c++14 的 Cygwin 上使用 g++4.9.2)。我想创建一个 class,其行为类似于 std::mem_fn。这是我的 class:

template <class R, class T, R(T::*P)() const >
struct property {

    static R get(const T& t) {
        return (t.*P)();
    }
};

其中R是return类型,T是我感兴趣的对象的类型。第三个模板参数是指向成员函数的指针。到目前为止,还不错。

然后我创建一个简单的 class,它包含一个整数,如下所示

class data_class {

public:

    unsigned get_data() const {
        return m_data;
    }

private:
    unsigned m_data;
};

这是 class,将在前面显示的 property class 中使用。

现在我创建了两个继承自 data_class 的 class,如下所示

struct my_classA
: public data_class {

    using data = property<unsigned, data_class, &data_class::get_data>;
};

//same as my_classA, only templated
template <int I>
struct my_classB
: public data_class {

    using data = property<unsigned, data_class, &data_class::get_data>;
};

它们具有完全相同的内部 typedef,但 my_classB 是模板化的。现在以下类型在理论上应该是相同的:

using target_t = property<unsigned, data_class, &data_class::get_data>;
using test1_t = typename my_classA::data;
using test2_t = typename my_classB<1>::data;

但是我的编译器说只有 test1_ttarget_t 是相同的。 test2_t 推导的类型显然是

property<unsigned int, data_class, (& data_class::get_data)> >

这个类型在指向成员函数的指针周围有这些方括号。为什么 test2_ttarget_t 不同?如果您想在您的系统上试用,这里是完整的代码。非常感谢任何帮助。

#include <type_traits>

class data_class {

public:

    unsigned get_data() const {
        return m_data;
    }

private:
    unsigned m_data;
};

//takes return type, class type, and a pointer to member function
//the get function takes an object as argument and uses the above pointer to call the member function
template <class R, class T, R(T::*P)() const >
struct property {

    static R get(const T& t) {
        return (t.*P)();
    }
};

struct my_classA
: public data_class {

    using data = property<unsigned, data_class, &data_class::get_data>;
};

//same as my_classA, only templated
template <int I>
struct my_classB
: public data_class {

    using data = property<unsigned, data_class, &data_class::get_data>;
};

//used to produce informative errors
template <class T>
struct what_is;

//all 3 types below should, in theory, be the same
//but g++ says that test2_t is different
using target_t = property<unsigned, data_class, &data_class::get_data>;
using test1_t = typename my_classA::data;
using test2_t = typename my_classB<1>::data;

static_assert(std::is_same<target_t, test1_t>::value, ""); //this passes
static_assert(std::is_same<target_t, test2_t>::value, ""); //this does not

int main() {

    what_is<test1_t> t1;
    what_is<test2_t> t2;
}

我 运行 你的代码是用 c++11 编写的,因为我对 c++14 还不是很熟悉。但我所替换的只是使用(别名)和 typedef 并稍微简化了代码。不会影响其输出。

我通过将类型名称 T 添加到继承的 classB 模板中获得了所需的结果,该模板在实例化时会将 R 替换为 T,因此在本例中 "unsigned".

#include <iostream>
#include <type_traits>

template <typename R, typename T, R(T::*P)() const>
struct property
{
    static R get(const T& t)
    {
        return (t.*P)();
    }
};


struct data_class
{
    private:
        unsigned m_data;

    public:
        unsigned get_data() const
        {
            return m_data;
        }
};

struct my_classA : public data_class
{
    typedef property<unsigned, data_class, &data_class::get_data> data;
};

template <typename T, int>
struct my_classB : public data_class
{
    typedef property<T, data_class, &data_class::get_data> data;
};


int main()
{

    typedef typename my_classA::data normClassA;
    typedef typename my_classB<unsigned,1>::data tmplClassB;

    std::cout<< std::is_same< property<unsigned, data_class, &data_class::get_data> , normClassA >::value <<std::endl;
    std::cout<< std::is_same< property<unsigned, data_class, &data_class::get_data> , tmplClassB >::value <<std::endl;

}

结果是这样的:

~$g++ -std=c++11 test.cpp
~$./a.out 
1
1

我认为问题与 class 模板实例化标准有关,因为当我最初尝试打印两个 classes 的 sizeof 时,my_classA::data 返回 1,但是my_classB<1>::数据以编译器错误结束。对于为什么会发生这种情况,我仍然很模糊。从技术上讲,它应该已经很好地实例化了 class 模板。可能是 classB 模板中的 属性 被错误地实例化了。我会对此进行更多研究,但如果您找到答案,请 post 找到答案。很有意思!

编辑: 原始代码在 Cygwin GCC 4.8.2 上运行良好。结果是1和1。可能只是gcc4.9.2编译器的问题。