Lütfen dikkat, bu davranışı açıklamanıza yardımcı olun. Aşağıda sunduğum örnek, düşünebildiğim en basit olanıdır, ancak problemi özetlemektedir (C++ 14 ile C++ 14 etkinleştirilmiş g ++ 4.9.2'yi kullanarak). std::mem_fn
'a benzer davranacak bir sınıf oluşturmak istiyorum. İşte benim sınıftır.İç yazım hatası, üst sınıflara bağlı olarak değişir
template <class R, class T, R(T::*P)() const >
struct property {
static R get(const T& t) {
return (t.*P)();
}
};
R
dönüş türüdür
ve T
ben ilginç duyuyorum nesne türündendir üçüncü şablon parametresi üye işlevine bir göstericidir. Çok uzak çok iyi.
Daha sonra bu daha önce gösterilen property
sınıf kullanılacak sınıftır
class data_class {
public:
unsigned get_data() const {
return m_data;
}
private:
unsigned m_data;
};
aşağıdaki gibi bir tam sayıyı tutan basit bir sınıfı oluşturur.
Şimdi Onlar aynı iç typedef sahip
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>;
};
şöyle data_class
devralan iki sınıfları oluşturmak, ama my_classB
şablon olarak değiştirilebilir. Şimdi aşağıdaki türlerde teoride aynı olmalıdır:
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;
Ancak benim derleyici yalnızca test1_t
ve target_t
aynı olduğunu söylüyor. test2_t
için çıkarılabilir tip görünüşe
property<unsigned int, data_class, (& data_class::get_data)> >
olduğunu. Neden test2_t
target_t
ile aynı değil? Sisteminizde denemek istediğiniz durumda tam kod. Herhangi bir yardım çok takdir edilir.
#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;
}
Aslında * çok * karışık mesajlar almak için 'test2_t' (bu tür bir değişken bildirme gibi) kullanmaya çalışın:" error: '& data_class :: get_data', "unsigned int" için geçerli bir şablon argümanı değil (data_class: : *)() const '"," hata:' & X :: Y 'biçimindeki bir işaretçi-üye olmalıdır. – hvd
GCC <= 4.8 ve Clang'da çalışır. 4.9+ yılında bir gerileme olarak görünüyor. –
@ T.C: Bunun için teşekkürler. Hata raporuna bağlantı verebilir misiniz lütfen? Ayrıca bir yanıt olarak gönderin, böylece kabul edebilirim :) – linuxfever