我正在製作可變位大小像素顏色值的類。不管怎麼說,我得到了它的工作,但有一個奇怪的現象:在我標線錯誤無法訪問時,爲什麼此程序無法編譯?
#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>();
然後我得到的錯誤
cannot convert from c<uint32_t> to c<uint8_t>
這是沒有意義的,因爲它是不應該,如果任何東西轉換它的同類型這是不符合uint32_t
到的情況下
這是在MSVC上,有誰知道爲什麼會發生?
'不能從c轉換爲c '呃,你不能,實際上不清楚的是什麼? –
你的「if」陳述中是否真的有兩個回報?或者是一個窮人的評論? – erip
@πάνταῥεῖ該場景永遠不會發生 – user81993