Regarding the example of cppreference, I don't understand this error.Both should be initialized with two constructors of arguments, but I would appreciate it if you could tell me why the first std::
is an error.
Uniform Initialization - cpprefjp C++ Japanese Reference
#include<iostream>
# include <vector>
# include <iterator>
int main()
{
// Compilation error! considered function declaration syntax but error because parameter name (std::cin) is namespace-qualified
// std::vector<char>vec(std::istream_iterator<char>(std::cin),
// std::istream_iterator<char>());
// vec is the variable of type std::vector<char> initialized with two constructors.
std::vector<char>vec { std::istream_iterator<char>(std::cin),
std::istream_iterator<char>()};
}
The C++ language is compatible with previous versions.If C++11 introduces uniform initialization using curly braces {}
, it does not change the behavior of parentheses ()
.
// Compilation error! considered function declaration syntax but error because parameter name (std::cin) is namespace-qualified
std::vector<char>vec(std::istream_iterator<char>(std::cin),
std::istream_iterator<char>());
has nothing to do with C++11 uniform initialization and causes compilation errors in previous versions.Specifically
int func(int param1, int param2);
In the same format as , the compiler interprets this line as a prototype declaration of a function.Argument names param1
and param2
are optional in the prototype declaration.Now
std::vector<char>
Suitable for type namevec
→appropriate identifierparam1
type:std::istream_iterator<char>(std::cin)
→Incorrect type nameparam2
type:std::istream_iterator<char>()
→Incorrect type nameThis results in a compilation error.
Braces ()
have the disadvantage of being interpreted as a prototype declaration for a function, but braces {}
uniform initialization does not have this disadvantage because it has different syntax.
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
610 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.