The code below uses an empty C + 11 style initializer. At startup, the result is std::vectorone containing one element, which was apparently constructed by default.
This is clearly an artificial case, and there are better ways to construct an empty vector. Nonetheles, behavior is contrary to intuition. Is this a compiler / C ++ runtime library error?
I suspect that one of the std::vectorother constructors is actually being called here.
#include <iostream>
#include <memory>
#include <vector>
int main(int argc, const char * argv[])
{
typedef std::vector<std::shared_ptr<int>> Container;
Container c{{}};
std::cout << "Vector size is: " << c.size() << std::endl;
for (auto item: c)
{
std::cout << "Item: " << item.get() << std::endl;
}
}
Output:
Vector size is: 1
Item: 0x0
Program ended with exit code: 0
Compiler:
$ clang --version
Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin12.4.0
marko source
share