9
Sorun, farklı derleyicilerin farklı çıktılar üretmesidir (clang/gcc) ve bu nedenle bu kullanımın tanımlanmamış bir davranış olduğunu düşünmemi sağlar. Ancak amacım referans atarken const
sonucunu çıkarmaktır.Operatörden const (const) algılama T &()
Çıktı ile:
clang-3.6 ->not const
gcc-4.8.4 ->const
#include <iostream>
#include <type_traits>
struct AnyReference {
template <typename RT> AnyReference(RT &a_var) : _ptr(&a_var) {}
template <typename T> operator T &() const
{
if (std::is_const<T>::value) {
std::cout << "const\n";
}
else {
std::cout << "not const\n";
}
return *reinterpret_cast<T *>(_ptr);
}
void *_ptr;
};
int main()
{
int i(5);
AnyReference a(i);
const int &c = a;
}
sana okuyalım Bu gönderiyi takip edebilirsiniz: http://stackoverflow.com/questions/32515183/const-auto-stdinitializer-list-difference-between-clang-and-gcc –
Her iki operayı da sağlamayı düşündünüz mü tor T & 've' operatör yapısı T & '? –
@Ben Voigt Hayır Yok, ama bu harika bir çözüm! Beklendiği gibi çalışır. –