Reputation: 188
Am I right to interpret unsigned int{};
as zero-initialization of a temporary unsigned int
without a name in the contexts provided below? The example does not work with clang or gcc, but compiles fine in Visual Studio: https://godbolt.org/z/LYWCCN
int ifun(int value) {
return value * 10;
}
unsigned int uifun(unsigned int value) {
return value * 10u;
}
int main() {
// does not work in gcc and clang
unsigned int{};
unsigned int ui = uifun(unsigned int{});
// works with all three compilers, also unsigned{}; works
int{};
int i = ifun(int{});
}
Upvotes: 4
Views: 119
Reputation: 172894
Clang and Gcc are right. unsigned int{};
is considered as explicit type conversion firstly, which only works with single-word type name; while unsigned int
(and int*
etc) is not but int
is.
(emphasis mine)
5) A single-word type name followed by a braced-init-list is a prvalue of the specified type
designating a temporary (until C++17)
whose result object is (since C++17)
direct-list-initialized with the specified braced-init-list.
As the workaround, you can
using unsigned_int = unsigned int;
unsigned_int{};
As the result it'll value-initialize (zero-initialize) a temporary unsigned int
as you expected.
Upvotes: 3
Reputation: 206577
You can use a type alias to get around the limitation.
using UI = unsigned int;
UI{};
UI ui = uifun(UI{});
Upvotes: 2