-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Closed
Description
Hi there!
Try guess the output of the following:
#include <iostream>
#include <cassert>
#include <vector>
#include <json.hpp>
// for convenience
using json = nlohmann::json;
template <typename T>
class WeirdClass {
public:
WeirdClass(const std::vector<T> &v) : vec{v}
{
std::cout << "v.size() == " << v.size() << std::endl;
std::cout << "vec.size() == " << vec.size() << std::endl;
std::cout << std::endl;
for (decltype(v.size()) idx = 0; idx != v.size(); ++idx) {
std::cout << "v[" << idx << "] == " << v[idx] << std::endl;
}
std::cout << std::endl;
for (decltype(vec.size()) idx = 0; idx != vec.size(); ++idx) {
std::cout << "vec[" << idx << "] == " << vec[idx] << std::endl;
}
std::cout << std::endl;
assert(v.size() == vec.size());
}
private:
const std::vector<T> vec;
};
int main()
{
json x, y;
x["value"] = 0;
y["value"] = 1;
WeirdClass<json> weird{{x, y}};
return 0;
}It aborts!!!!! and gives me this
v.size() == 2
vec.size() == 1
v[0] == {"value":0}
v[1] == {"value":1}
vec[0] == [{"value":0},{"value":1}]
go: /home/me/main.cpp:31: WeirdClass<T>::WeirdClass(const std::vector<_RealType>&) [with T = nlohmann::basic_json<>]: Assertion `v.size() == vec.size()' failed.
AbortedChanging the constructor to
WeirdClass(const std::vector<T> &v) : vec{v.cbegin(), v.cend()}
{ //...gives me the expected output:
v.size() == 2
vec.size() == 2
v[0] == {"value":0}
v[1] == {"value":1}
vec[0] == {"value":0}
vec[1] == {"value":1}What's going on here??
I thought things like the copy constructor should behave in a standard way. (Or is my compiler buggy?? I'm using: gcc c++ (Debian 4.9.3-3) 4.9.3; and compiling with -std=c++14)
Thanks
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels