gpt4 book ai didi

c++ - 内部 typedef 的变化取决于父类

转载 作者:可可西里 更新时间:2023-11-01 16:23:58 26 4
gpt4 key购买 nike

我放弃了,请帮忙解释一下这个行为。我在下面给出的示例是我能想到的最简单的示例,但它总结了问题(在启用了 c++14 的 Cygwin 上使用 g++4.9.2)。我想创建一个行为类似于 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 是返回类型,T 是我感兴趣的对象的类型。第三个模板参数是指向成员函数的指针。到目前为止,还不错。

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

class data_class {

public:

unsigned get_data() const {
return m_data;
}

private:
unsigned m_data;
};

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

现在我创建了两个继承自 data_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 并稍微简化了代码。不会影响其输出。

我通过向继承的 classB 模板添加类型名称 T 获得了预期的结果,该模板在实例化时会用 T 替换 R,因此在本例中为“无符号”。

#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

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

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

关于c++ - 内部 typedef 的变化取决于父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29781966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com