Related
I am using std::variant which is a C++17 feature. But as far as I am aware g++ should support this. I am using g++ version 9.2.1.
The first error summarised is:
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_object’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.cpp:1:
json20.hpp:17:8: note: forward declaration of ‘struct json_object’
17 | struct json_object {
| ^~~~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
I am compiling just json.cpp like this:
g++ -c -std=c++17 json20.cpp
This compiles ok on Visual Studio 2017. Although there I had to set the compiler to use c++17 standard. So I assume it should work on g++. Or is there an error in my code?
Here are the source files:
json20.hpp:
#ifndef JSON20_HPP_
#define JSON20_HPP_
#include <variant> // type-safe union
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include "json_value.hpp"
struct json_array {
std::vector<json_value> array;
};
struct json_object {
std::unordered_map < std::string, json_value> object;
json_value& operator [](std::string key) {
return object[key];
}
};
struct json_null {
};
std::ostream& operator<< (std::ostream& os, json_value const& v);
class json20 {
public:
/* default constructor */
json20() {}
/* construct json object from input stream */
json20(std::istream& istrm, std::string& parse_status);
/* parent node of json object */
json_value root;
bool insert(const json_object& object);
private:
bool parse(std::istream& istrm, std::string& parse_status);
json_value parse_json(std::istream& istrm, json_value& value, std::string& parse_status);
};
#endif // JSON20_HPP_
json20.cpp:
#include "json20.hpp"
std::ostream& operator<< (std::ostream& os, json_value const& v) {
return os;
}
json20::json20(std::istream& istrm, std::string& parse_status) {
}
json_value json20::parse_json(std::istream& istrm, json_value& value, std::string& parse_status) {
return value;
}
bool json20::parse(std::istream& istrm, std::string& parse_status) {
return true;
}
bool json20::insert(const json_object& object) {
return true;
}
json_value.hpp:
#ifndef JSON_VALUE_HPP_
#define JSON_VALUE_HPP_
#include "json20.hpp"
#include <variant>
#include <string>
#include <unordered_map>
// fwd declare
struct json_array;
struct json_object;
struct json_null;
typedef std::variant<double, bool, std::string, json_array, json_object, json_null> json_value;
#endif // JSON_VALUE_HPP_
test.cpp:
#include "json20.hpp"
#include <string>
#include <iostream>
#include <sstream>
#include <unordered_map>
int main() {
const std::string s = "my list";
double arrayofdoubles[] = {1.0, 2.0, 3.0};
json_array jarray;
for (auto i : arrayofdoubles) {
jarray.array.push_back(i);
}
json_object obj;
obj.object[s] = jarray;
json20 json;
json.insert(obj);
//// output to stdout
std::ostringstream ostrm;
ostrm << json.root;
std::string expected("{\"my list\":[1,2,3]}");
std::cout << json.root;
}
The complete error message is huge and I am only able to provide a part of it due to stackoverflow size limits:
acomber#mail:~/Documents/projects/json20$ g++ -c -std=c++17 json20.cpp
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::is_trivially_destructible<json_object>’:
/usr/include/c++/9/type_traits:2959:25: required from ‘constexpr const bool std::is_trivially_destructible_v<json_object>’
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_object’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.cpp:1:
json20.hpp:17:8: note: forward declaration of ‘struct json_object’
17 | struct json_object {
| ^~~~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_destructible_v<json_object>’:
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: required from ‘class std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/bits/stl_pair.h:215:11: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:2959:25: error: ‘value’ is not a member of ‘std::is_trivially_destructible<json_object>’
2959 | inline constexpr bool is_trivially_destructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::is_trivially_destructible<json_null>’:
/usr/include/c++/9/type_traits:2959:25: required from ‘constexpr const bool std::is_trivially_destructible_v<json_null>’
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:1235:12: error: invalid use of incomplete type ‘struct json_null’
1235 | struct is_trivially_destructible
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from json20.hpp:11,
from json20.cpp:1:
json_value.hpp:13:8: note: forward declaration of ‘struct json_null’
13 | struct json_null;
| ^~~~~~~~~
In file included from /usr/include/c++/9/variant:36,
from json20.hpp:5,
from json20.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_destructible_v<json_null>’:
/usr/include/c++/9/variant:286:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_dtor’
/usr/include/c++/9/variant:295:4: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types> using _Move_assign_alias = std::__detail::__variant::_Move_assign_base<std::__detail::__variant::_Traits<_Types>::_S_trivial_move_assign, _Types ...> [with _Types = {double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null}]’
/usr/include/c++/9/variant:681:12: required from ‘struct std::__detail::__variant::_Variant_base<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/variant:1219:11: required from ‘class std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>’
/usr/include/c++/9/bits/stl_pair.h:215:11: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/bits/hashtable_policy.h:233:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12: required from ‘struct std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11: required from ‘class std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >, std::allocator<std::pair<const std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> > >, std::__detail::_Select1st, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<std::basic_string<char>, std::variant<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null> >’
json20.hpp:18:48: required from here
/usr/include/c++/9/type_traits:2959:25: error: ‘value’ is not a member of ‘std::is_trivially_destructible<json_null>’
2959 | inline constexpr bool is_trivially_destructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::__is_trivially_move_constructible_impl<json_object, true>’:
/usr/include/c++/9/type_traits:1185:12: required from ‘struct std::is_trivially_move_constructible<json_object>’
/usr/include/c++/9/type_traits:2947:25: required from ‘constexpr const bool std::is_trivially_move_constructible_v<json_object>’
/usr/include/c++/9/variant:290:5: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_ctor’
/usr/include/c++/9/variant:295:23: required from ‘constexpr const bool std::__detail::__variant::_Traits<double, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, json_array, json_object, json_null>::_S_trivial_move_assign’
/usr/include/c++/9/variant:677:11: required by substitution of ‘template<class ... _Types>
Something interesting is that if I replace std::unordered_map with std::map then it compiles ok. Why?
I can't figure out how to do it; it's driving me nuts.
#include <iostream>
#include <memory>
#include <map>
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
map["foo"] = std::move(bar);
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
This compiles just fine on gcc v4.9.2:
$ g++ -std=c++0x test.cc -o test
Unfortunately, all I have available to me is gcc v4.4.7, which produces a hideous error message that I'll stick at the bottom.
I can push_back unique_ptrs into vectors just fine; I don't understand why there's an issue with map.
I've also tried using insert:
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
auto pair = std::make_pair("foo", std::move(bar));
map.insert(std::move(pair));
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
I can make the pair just fine, but when I try to move it into insert I get the error. It appears that it's trying to call the copy constructor for some reason, but I don't understand why, since I'm moveing it.
emplace has the same result:
int main()
{
std::map<std::string, std::unique_ptr<std::string>> map;
std::unique_ptr<std::string> bar(new std::string("bar"));
auto pair = std::make_pair("foo", std::move(bar));
map.emplace(std::move(pair));
std::cout << "foo: " << *(map["foo"]) << std::endl;
}
Both of these compile fine in g++ v4.9.2, but not in v4.4.7.
Anyone have any ideas on an alternative way to do this (in v4.4.7)?
Full error message output (from the first example):
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:41,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
from test.cc:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/unique_ptr.h: In copy constructor 'std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:68: instantiated from 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, _Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Tp = std::_Rb_tree_node<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:394: instantiated from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:881: instantiated from 'std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:1215: instantiated from 'std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree_const_iterator<_Val>, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:540: instantiated from 'typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, const std::pair<const _Key, _Tp>&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:450: instantiated from '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]'
test.cc:9: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/unique_ptr.h:214: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp_Deleter = std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:68: error: used here
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/map:60,
from test.cc:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h: In constructor 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:136: note: synthesized method 'std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::pair(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)' first required here
GCC 4.4.7 does not provide full support for C++ 11 features, so it is unrealistic to expect all C++ 11 features to work properly. In this particular case, the compiler is not generating move assignment and move copy methods for you. So, when std::map attempts assignment or copy, it uses the regular default ones, which are deleted for unique_ptr.
If you just need a smart pointer, you can use shared_ptr instead.
std::map<std::string, std::shared_ptr<std::string>> map;
std::shared_ptr<std::string> bar = std::make_shared<std::string>("bar");
map["foo"] = bar;
I have looked into the issue. STL implementation for map correctly has a move constructor defined in it:
#ifdef __GXX_EXPERIMENTAL_CXX0X__
template<typename... _Args>
_Rb_tree_node(_Args&&... __args)
: _Rb_tree_node_base(),
_M_value_field(std::forward<_Args>(__args)...) { }
#endif
};
Moreover, this is called, as stated in the error output. std::pair has move constructor defined as well, but this one is not called.
I believe, std::forward and deduced type of Args is at fault:
usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:
In constructor ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...)
[with _Args = const std::pair<..., std::unique_ptr<...> >&,
As you see, type of _Args is a const reference, which is forwarded by forward, and regular pair constructor is called.
I assume, one might fiddle with stl_tree.h to fix it - for example, by removing std::forward and just casting the arg to rvalue reference. This would not be correct, of course, but might work for specific case.
I can't find what I'm doing wrong here. The function eta does what I ask but when I use it in the loop I get the attached error.
bool eta(map<string, TLorentzVector> map_jets, string jet){
return( fabs(map_jets[jet].PseudoRapidity()) > 2.5 );
}
and then
vector<pair<string,double> > jets_pt( vec_jets.size() );
for( vector<pair<string,double> >::iterator it = jets_pt.begin(); it != jets_pt.end(); ++it)
jets_pt.erase(remove_if(jets_pt.begin(),jets_pt.end(),eta(map_jets,it1->first)),jets_pt.end);
I get the error
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _OutputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1291: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1216: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:338: instantiated from '_InputIterator std::find_if(_InputIterator, _InputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1287: instantiated from '_ForwardIterator std::remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>*, std::vector<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> > > >, _Predicate = bool]'
/misc/cdf/gbertoli/hww/Diboson_v20_taus/Ana/src/Functions.cc:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:260: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:264: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:268: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:272: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:280: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:284: error: '__pred' cannot be used as a function
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:288: error: '__pred' cannot be used as a function
mv: cannot stat `/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/srt_dep_tmp.27294': No such file or directory
gmake[2]: *** [/misc/cdf/gbertoli/hww/Diboson_v20_taus/tmp/Linux2.6-GCC_4_1/Ana/libAna-shared/Functions.o] Error 1
gmake[1]: *** [src.lib] Error 2
gmake: *** [Ana.all] Error 2
First off, the only valid signature for remove_if takes a single function as the predicate argument:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), eta),
jets_pt.end());
This means that eta must be a function returning bool and taking precisely one argument whose type is the value type of the container:
bool eta(const std::pair<string, double> & p)
{
// do something useful with p
}
If this doesn't fit your bill because you need additional state information in the predicate, then you need to make it a function object:
struct Eta
{
const std::map<string, TLorentzVector> & map_jets;
Eta(const std::map<string, TLorentzVector> & m) : map_jets(m) { }
bool operator()(const std::pair<string, double> & p) const
{
std::map<string, TLorentzVector>::const_iterator it = map_jets.find(p->second);
return it == map_jets.end() ?
false :
std::fabs(it->second.PseudoRapidity()) > 2.5;
}
};
Now you have to use remove_if with an instance of Eta:
jets_pt.erase(std::remove_if(jets_pt.begin(), jets_pt.end(), Eta(map_jets)),
jets_pt.end()); // ^^^^^^^^^^^^^
Note that your use of the for loop is extremely suspicious; you should double-check that.
std::remove_if takes a value of the contained type (bool pred (container::value_type), or a functor which overloads operator() appropriately).
For std::map, you have to do it like explained here remove_if equivalent for std::map .
You're passing a bool (the results of calling eta) to remove_if as
the third argument. You need to pass it a predicate: a pointer to a
function taking a single argument and returning something which can be
converted to a bool, or a functional object. In the error messages,
__pred is your third argument; remove_if calls it here with a
pair<string, double>, and expects a bool in return. You have to
provide something which can be called like this. (You might want to
look into boost::bind.)
And BTW: passing a map by value to eta might not be a good idea.
It's going to slow things down considerably if the map is big.
The following code supposedly works with VS 2008 but I'm having trouble around line 53
where:
portMap_.erase(it);
I'm using codeblocks/mingw/gcc 4.xx
#include<iostream>
#include<map>
#include<string>
using namespace std;
//defining a union that is used with newMap_
union uu
{
char c;
int i;
} u;
//Lets define two different maps
//The first parameter is key and second value
map<string, int> portMap_;
map<void *, uu> newMap_;
int main()
{
//first entry in portmap
portMap_["first"] = 1;
//example of using the iterator
map<string, int>::const_iterator it;
string z = "second";
it = portMap_.find(z); //not in the map so wont be found
if(it == portMap_.end())
{
portMap_[z] = 22; //add second element
}
//Add thrid element directly
z = "third";
portMap_[z] = 12345;
//Add 4th element by insert
portMap_.insert(pair<string,int>("fourth", 4444));
//Add 5th element by insert
portMap_.insert(pair<string,int>("fifth", 5555));
cout<<"\n** Printing the portmap_ values **"<<endl;
for(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;
cout<<"\n** Removing fourth element **"<<endl;
z = "fourth";
it = portMap_.find(z);
portMap_.erase(it);
cout<<"\n** Printing the portmap_ values **"<<endl;
for(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;
//Playing with New Map
cout<<"\n\nCreating New Map whose key is a void pointer"<<endl;
uu u_val1, u_val2;
void *val1, *val2;
u_val1.i = 70, val1 = &u_val1;
newMap_[val1]=u_val1;
val2 = val1;
map<void *, uu>::const_iterator it_new;
it_new = newMap_.find(val2);
if(it_new != newMap_.end())
{
u_val2 = it_new->second;
cout<<"Note that since u_val2 is a union you can print i or c as required"<<endl;
cout<<"val2 = "<<val2<<" value.c = "<<u_val2.c<<endl;
cout<<"val2 = "<<val2<<" value.i = "<<u_val2.i<<endl;
}
return 0;
}
Heres the error:
map_example\map_example.cpp||In function 'int main()':|
map_example\map_example.cpp|51|error: no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >::erase(std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> >&)'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|566|note: candidates are: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char,|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|581|note: typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::size_type std::map<_Key, _Tp, _Compare, _Alloc>::erase(const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_stri|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|596|note: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::all|
||=== Build finished: 1 errors, 0 warnings ===|
Your iterator is defined as const. Try a non-const iterator.
You can also erase an element by simply using a key_type. In your example, it would be:
portMap_.erase(z);
Try change from map<string, int>::const_iterator it; to map<string, int>::iterator it.
How would I best implement these? I thought of something like this:
using namespace std;
shape_container
shape_container::clone_deep () const
{
shape_container* ptr = new shape_container();
copy( data.begin(), data.end(), (*ptr).begin() );
return *ptr;
}
shape_container
shape_container::clone_shallow () const
{
return *( new shape_container(*this) );
}
The member data is defined as follows:
std::map<std::string, shape*> data;
This doesn't work, unfortunately. Here's the compiler errors, I don't really understand them:
g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -I./libfglwin/include/ -I. -c shape_container.cpp -o shape_container.o
/usr/include/c++/4.2.1/bits/stl_pair.h: In member function ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>&)’:
/usr/include/c++/4.2.1/bits/stl_pair.h:69: instantiated from ‘static _OI std::__copy<<anonymous>, <template-parameter-1-2> >::copy(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, <template-parameter-1-2> = std::bidirectional_iterator_tag]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:315: instantiated from ‘_OI std::__copy_aux(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:340: instantiated from ‘static _OI std::__copy_normal<<anonymous>, <anonymous> >::__copy_n(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, bool <anonymous> = false]’
/usr/include/c++/4.2.1/bits/stl_algobase.h:401: instantiated from ‘_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OutputIterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >]’
shape_container.cpp:70: instantiated from here
/usr/include/c++/4.2.1/bits/stl_pair.h:69: error: non-static const member ‘const std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::first’, can't use default assignment operator
/usr/include/c++/4.2.1/bits/stl_algobase.h: In static member function ‘static _OI std::__copy<<anonymous>, <template-parameter-1-2> >::copy(_II, _II, _OI) [with _II = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, _OI = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*> >, bool <anonymous> = false, <template-parameter-1-2> = std::bidirectional_iterator_tag]’:
/usr/include/c++/4.2.1/bits/stl_algobase.h:268: note: synthesized method ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, shape*>&)’ first required here
Somehow this looks unnecessarily complicated to me. Is that
true and can I make it better?
BTW, I have clone() methods
in the classes I derived from shape. Perhaps I can use them
for the clone_deep method? Are they ok? They look something
like this:
class shape
{
public:
/* Many methods. */
virtual shape* clone () const = 0;
protected:
colorRGB color_;
std::string name_;
};
class triangle2d : public shape
{
public:
/* Many methods. */
triangle2d* clone() const;
private:
point3d a_, b_, c_;
};
triangle2d*
triangle2d::clone() const
{
return new triangle2d(*this);
}
Usually a clone function would return a pointer to a new instance. What you are returning is an object by value which is copy constructed from a dynamically allocated isntance that is then leaked.
If you want to return by value then you should not use new.
E.g.
shape_container shape_container::clone_shallow () const
{
return *this;
}
If the data member is just a std::map instance, then it will be copied as part of your shallow clone in any case so there is no need to do the std::copy in the deep clone case, it's not trying to do anything different.
If you wanted to do a std::copy of a map you would need to use a std::insert_iterator.
I think that it might be easier to do a clone of each shape after the fact, though.
e.g.
shape_container shape_container::clone_deep() const
{
shape_container ret(*this);
for (std::map<std::string, shape*>::iterator i = ret.data.begin(); i != ret.data.end(); ++i)
{
i->second = i->second->clone();
}
return ret;
}
First of all your example leaks memory because you new a shape_container in your methods but then it gets copied out through the return value. You should be returning pointers as with your shape example.
The compiler errors look to be related in some way to the copying since it's complaining it can't generate an assignment operator for you. Again, try using pointers and that issue should go away.
If you do deep copy of map then you have to a new create map with all element with deep copy.
Think about reference counting approach , it will be better approach.
One option is to wrap your shape type in a type that performs a deep
copy of the object:
class shape_deep_copy_wrapper {
// ...
public:
shape_deep_copy_wrapper (shape * shape)
: m_my_shape (shape)
{
}
shape_deep_copy_wrapper (shape_deep_copy_wrapper const & rhs)
: m_my_shape (rhs.m_my_shape.deep_copy ())
{
}
// ...
private:
shape * m_my_shape;
};
Then construct a map with this type:
typedef std :: map < shape_deep_copy_wrapper , ... > DeepCopy ;
typedef std :: map < shape* , ... > ShallowCopy ;