Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::byte curly bracket type

28 views
Skip to first unread message

Mut...@dastardlyhq.com

unread,
Jan 20, 2023, 11:19:49 AM1/20/23
to
I've found that with the std::byte type of C++ 17 it seems it'll only
be constructed with {<value>} which is not an initialiser list. Eg:

fenris$ cat t.cc
#include <cstddef>

using namespace std;

int main()
{
std::byte b{123};
std::byte c(123);
std::byte d({123});
}
fenris$ c++ -std=c++17 t.cc
t.cc:8:12: error: cannot initialize a variable of type 'std::byte' with an
rvalue of type 'int'
std::byte c(123);
^ ~~~
t.cc:9:12: error: cannot initialize non-class type 'std::byte' with a
parenthesized initializer list
std::byte d({123});
^ ~~~~~
2 errors generated.

So if the constructor parameter is not a simple rvalue - templated or not - or
an intialiser list then what the hell is it?

Bo Persson

unread,
Jan 20, 2023, 11:25:53 AM1/20/23
to
It is a "list initialization" that specifically works for enumerations,
like enum class byte.

https://en.cppreference.com/w/cpp/language/enum#enum_relaxed_init_cpp17

Mut...@dastardlyhq.com

unread,
Jan 20, 2023, 11:45:07 AM1/20/23
to
So its enumerated 0-255? Whats the point when a compiler will warn you if
you try to assign a too large or negative value to a uint8_t anyway?
Another C++17 solution looking for a problem IMO.

Thanks anyway.

Öö Tiib

unread,
Jan 21, 2023, 7:38:38 AM1/21/23
to
It was indeed another addition to standard library that failed.
Strictly it is enumerated variant of unsigned char (that can have more bits than 8).
Committee keeps adding features that do not satisfy any of needs in industry.
Sole bonus of std::byte was that annoying IDB of enumerated types upgraded
to UB by C++17 was made well defined at least for unsigned enums in C++20.

Mut...@dastardlyhq.com

unread,
Jan 21, 2023, 11:10:33 AM1/21/23
to
Well quite.

>Sole bonus of std::byte was that annoying IDB of enumerated types upgraded
>to UB by C++17 was made well defined at least for unsigned enums in C++20.

On a purely technical note, I assumed the definition would be rather tortuous
along the lines of:

enum class byte
{
a = 0,
b = 1,
:
etc
:
};

But in the clang header it simply does:

enum class byte : unsigned char {};

Which is new syntax to me, didn't realise you could do that. Not sure what
the curly brackets signify in this case but I learnt something new anyway.

Richard Damon

unread,
Jan 21, 2023, 11:24:15 AM1/21/23
to
The curly braces are around the enumaration values of the enum, just
like a regular enum.

the ": unsigned char" declares the underlying type for the enum, so it
is defined to have that specific size.

Mut...@dastardlyhq.com

unread,
Jan 21, 2023, 11:35:08 AM1/21/23
to
Sorry, I meant what is the point of the curly brackets after "unsigned char"
in the definition. You can put identifiers between them but they seem to be
ignored and it fails to compile if you try to use them. eg:

fenris$ cat t.cc
enum class mybyte : unsigned char {a,b,c};

int main()
{
mybyte b{1};
mybyte c{a};
}
fenris$ c++ -std=c++17 t.cc
t.cc:6:11: error: use of undeclared identifier 'a'
mybyte c{a};
^
1 error generated.
fenris$



Mut...@dastardlyhq.com

unread,
Jan 21, 2023, 11:40:45 AM1/21/23
to
On Sat, 21 Jan 2023 16:34:55 -0000 (UTC)
Mut...@dastardlyhq.com wrote:
>On Sat, 21 Jan 2023 11:23:54 -0500
>Sorry, I meant what is the point of the curly brackets after "unsigned char"
>in the definition. You can put identifiers between them but they seem to be
>ignored and it fails to compile if you try to use them. eg:
>
>fenris$ cat t.cc
>enum class mybyte : unsigned char {a,b,c};
>
>int main()
>{
> mybyte b{1};
> mybyte c{a};
>}
>fenris$ c++ -std=c++17 t.cc
>t.cc:6:11: error: use of undeclared identifier 'a'
> mybyte c{a};
> ^
>1 error generated.
>fenris$

I realise replying to yourself is bad form but I was being thick - I needed
to qualify the identifier. This works as expected outputting 1 and 2:

#include <iostream>

enum class mybyte : unsigned char {a,b,c};

int main()
{
mybyte b1{1};
mybyte b2{mybyte::c};

std::cout << (int)b1 << ", " << (int)b2 << std::endl;
}



Richard Damon

unread,
Jan 21, 2023, 11:46:16 AM1/21/23
to
since it is a class enum, the namespece of the labels are the enum,

so given

enum class mybyte : unsigned char { a, b, c};

you can do

mybyte b(mybyte::a);
0 new messages