Variable initialisation

I've come across this C++ 'teaser':

How many different ways can you find to define and initialise an int variable (excluding const/static/extern).

I've come up with 10! Does anyone know of any other ways?

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int a = 1;
	int b {2};
	int c {};
	int d (3);
	auto e {4};
	auto f = int(5);
	auto g {int(6)};
	auto h (7);
	auto i (int(8));
	auto j = 9;
}

If you have an hour to kill, here's a talk I attended at the CPP-on-Sea conference a couple of years ago, on the multiple different ways to initialise variables in C++:

https://youtu.be/SCoewvXablk

I haven't watched it since the conference, so I don't remember the details, but IIRC the speaker identifies a lot more than 10, although I think there was a certain amount of hair-splitting :)
just change the names lol.
int32_t
word
long
you can also exploit enum, #define, whatever else that defaults to int under the hood
Last edited on
Topic archived. No new replies allowed.