[英]“int” in Ubuntu
誰能告訴我,在編譯 c++ 程序時,g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 是否將int
s 更改為long int
s? 如果是這樣,如何改變? 如果沒有,我是否只是重載operator long
? 是不是就像重載運算符 uint32_t/uint64_t 一樣? 這似乎是一種不同類型的類型轉換(不是雙關語)。
這導致錯誤:
uint128_t.h: In function ‘std::ostream& operator<<(std::ostream&,
uint128_t)’:
uint128_t.h:593: error: conversion from ‘uint128_t’ to ‘long int’ is
ambiguous
uint128_t.h:83: note: candidates are: uint128_t::operator uint64_t()
uint128_t.h:79: note: uint128_t::operator uint32_t()
uint128_t.h:75: note: uint128_t::operator uint16_t()
uint128_t.h:71: note: uint128_t::operator uint8_t()
uint128_t.h:67: note: uint128_t::operator int()
uint128_t.h:63: note: uint128_t::operator char()
uint128_t.h:59: note: uint128_t::operator bool()
不,它沒有。 一個 int 是一個 int 是一個 int。 它是與long int
不同的類型。 至於你重載哪些算子,完全看你想做什么。 你還沒有真正描述過。
但是如果你想定義一個操作符來處理int
,那么你應該為int
重載它。 如果您希望它在long int
上工作,請為此定義一個重載。
你想做什么? 為什么你將uint128_t
傳遞給操作員,但詢問int
和long int
?
試試這個變化:
out = "0123456789abcdef"[size_t(rhs % div)] + out;
因為您提供了對各種無符號類型的轉換,但沒有提供對有符號整數的轉換。
是的,您可以定義到long int
的隱式轉換,只需以與所有其他轉換相同的方式定義operator long
。
最后,請注意,您選擇的結構名稱由 POSIX 保留,並且可能與標准庫 header stdint.h
的未來版本沖突。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.