私は可変ビットサイズのピクセルカラー値のクラスを作成しています。この*私はマークされた行にエラーに到達できないときにこのプログラムをコンパイルできないのはなぜですか?
#pragma pack(push, 1)
template <typename my_type>
struct c {
my_type x;
c() {}
c(my_type x) { this->x = x; }
template<typename target_type>
c<target_type> convert() {
if (std::is_same<my_type, target_type>::value) {
return *this; //<- doesn't work
return *reinterpret_cast<c<target_type>*>(this); //<- does work
}
int target_size = sizeof(((c<target_type>*)0)->x);
int my_size = sizeof(x);
if (my_size < target_size) {
return c<target_type>(x << (target_size - my_size) * 8);
}
my_type rounder = ((x >> (my_size - target_size) * 8 - 1) & 9) > 4;
return c<target_type>((x >> (my_size - target_size) * 8) + rounder);
}
};
#pragma pack(pop)
、私は返すことができるはずですが、私はそれを行うと、以下の試験を使用してコンパイルしようとした場合::とにかく、私はそれが働くが、奇妙なものがありました
c<uint8_t> a;
c<uint32_t> b(2147483647);
a = b.convert<uint8_t>();
、私はエラーuint32_t
の場合ではありませんその同じタイプの場合に何かを変換するために想定されていないので意味がありません
cannot convert from c<uint32_t> to c<uint8_t>
を取得
これはMSVCにありますが、なぜそれが起こるのか知っていますか?あなたがあなたの場合は
'cから変換できません〜'本当に不明な点はありますか? –
あなたは本当にあなたの 'if'文中に2つの返品を返しますか、それとも貧しい人のコメントですか? – erip
@πάνταῥεῖそのシナリオは決して起こりません – user81993