char buf[3]{};
I saw the code that says
In my opinion,
char buf[3]={0};
and so on, but if you look at the results, both methods are filled with zero and 3 bytes.
Is the code correct in terms of c++?
The compiler is gcc 4.4.7.
It depends on what you call "correct"...
This is the "initializer list" syntax introduced in c++11.
https://cpprefjp.github.io/lang/cpp11/initializer_lists.html
- Braces {}
can be empty
- Direct initialization does not write =
(where =
will cause copy initialization)
cc++03 does not accept this initializer list.
In Oira's one-chip microcomputer development environment, none of the questions that correspond to c++11 are correct.Similarly, the relatively new gcc is the correct way to write.
In gcc-7.1.0, this behavior was shown.
$g++-ccpp11init.cpp#Compiled without warning
$ g++-c-std=c++03cppinit.cpp
cppinit.cpp:3:16:warning:extended initializer lists only available with-std=c++11 or-std=gnu++11
char buf[3]{};
^
$
g++-ccpp11init.cpp
alerts in gcc-4.9.4
g++-c-std=c++11cpp11init.cpp
now has no warning.
You may or may not be correct or incorrect in speaking based on what year of c++ language specification.View questions tagged with c++03 or
© 2024 OneMinuteCode. All rights reserved.