Related
I cant for the love of god understand what this compiler error is trying to tell me:
Reproduced here on godbolt: https://godbolt.org/z/v3M5hEbdv
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <string>
#include <functional>
#include <vector>
class MyService {
public:
virtual ~MyService() = default;
virtual void bar(
std::function<void (std::vector<std::string>&&)> onSuccess
) = 0;
};
class MyServiceMock : public MyService
{
public:
MOCK_METHOD(
void,
bar,
(std::function<void(std::vector<std::string>&&)> onSuccess),
(override)
);
};
[[maybe_unused]]
void foo() {
testing::NiceMock<MyServiceMock> myService;
EXPECT_CALL(myService, bar(testing::_))
.Times(testing::Exactly(1))
.WillOnce(testing::DoAll(
testing::Return(),
testing::InvokeArgument<0>(
std::vector<std::string>({"hello", "world"})
)
));
}
int main() {
return 0;
}
The compiler error :
Output of x86-64 gcc 11.2 (Compiler #1)
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/unique_ptr.h:37,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/memory:76,
from /opt/compiler-explorer/libs/googletest/trunk/googletest/include/gtest/gtest.h:55,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple: In instantiation of 'struct std::tuple_element<0, std::tuple<> >':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/utility:118:11: required by substitution of 'template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 0; _Tp = std::tuple<>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1403:5: required by substitution of 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_UTypes ...> >&& std::get(const std::tuple<_UTypes ...>&&) [with long unsigned int __i = 0; _Elements = {}]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-more-actions.h:515:22: required by substitution of 'template<class ... Args> decltype (testing::internal::InvokeArgument(get<0>(std::forward_as_tuple((forward<Args>)(testing::internal::InvokeArgumentAction<index, Params ...>::operator()::args)...)), declval<const std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&>())) testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::operator()<Args ...>(Args&& ...) const [with Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2466:26: required by substitution of 'template<class _Fn, class ... _Args> static std::__result_of_success<decltype (declval<_Fn>()((declval<_Args>)()...)), std::__invoke_other> std::__result_of_other_impl::_S_test(int) [with _Fn = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; _Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2477:55: required from 'struct std::__result_of_impl<false, false, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2482:12: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:344:8: required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<void()>::_Callable<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__invoke_result<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&> >; _Tp = void; _Res = void; _ArgTypes = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:413:9: required by substitution of 'template<class _Functor, class, class> std::function<void()>::function(_Functor) [with _Functor = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >; <template-parameter-1-2> = void; <template-parameter-1-3> = <missing>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:906:30: required from 'struct std::__is_constructible_impl<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:911:12: required from 'struct std::is_constructible<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:466:7: required by substitution of 'template<class G, class> testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>::Action(G&&) [with G = const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; <template-parameter-1-2> = <missing>]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: required from 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]'
<source>:33:18: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: error: static assertion failed: tuple index must be in range
1360 | static_assert(__i < tuple_size<tuple<>>::value,
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: note: '(0 < ((long unsigned int)std::integral_constant<long unsigned int, 0>::value))' evaluates to false
In file included from /opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock.h:57,
from <source>:2:
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h: In instantiation of 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]':
<source>:33:18: required from here
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: error: could not convert 'std::get<1, testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >(((const testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >*)this)->testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::actions)' from 'std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >' {aka 'const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'} to 'testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>'
1061 | std::get<sizeof...(Actions) - 1>(actions)};
| ^
| |
| std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > {aka const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/unique_ptr.h:37,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/memory:76,
from /opt/compiler-explorer/libs/googletest/trunk/googletest/include/gtest/gtest.h:55,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple: In instantiation of 'struct std::tuple_element<0, std::tuple<> >':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/utility:118:11: required by substitution of 'template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 0; _Tp = std::tuple<>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1403:5: required by substitution of 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_UTypes ...> >&& std::get(const std::tuple<_UTypes ...>&&) [with long unsigned int __i = 0; _Elements = {}]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-more-actions.h:515:22: required by substitution of 'template<class ... Args> decltype (testing::internal::InvokeArgument(get<0>(std::forward_as_tuple((forward<Args>)(testing::internal::InvokeArgumentAction<index, Params ...>::operator()::args)...)), declval<const std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&>())) testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::operator()<Args ...>(Args&& ...) const [with Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2466:26: required by substitution of 'template<class _Fn, class ... _Args> static std::__result_of_success<decltype (declval<_Fn>()((declval<_Args>)()...)), std::__invoke_other> std::__result_of_other_impl::_S_test(int) [with _Fn = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; _Args = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2477:55: required from 'struct std::__result_of_impl<false, false, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:2482:12: [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:344:8: required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<void()>::_Callable<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::__invoke_result<testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&> >; _Tp = void; _Res = void; _ArgTypes = {}]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/std_function.h:413:9: required by substitution of 'template<class _Functor, class, class> std::function<void()>::function(_Functor) [with _Functor = testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >; <template-parameter-1-2> = void; <template-parameter-1-3> = <missing>]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:906:30: required from 'struct std::__is_constructible_impl<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:911:12: required from 'struct std::is_constructible<std::function<void()>, const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:466:7: required by substitution of 'template<class G, class> testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>::Action(G&&) [with G = const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&; <template-parameter-1-2> = <missing>]'
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: required from 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]'
<source>:33:18: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: error: static assertion failed: tuple index must be in range
1360 | static_assert(__i < tuple_size<tuple<>>::value,
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/tuple:1360:25: note: '(0 < ((long unsigned int)std::integral_constant<long unsigned int, 0>::value))' evaluates to false
In file included from /opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock.h:57,
from <source>:2:
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h: In instantiation of 'testing::internal::DoAllAction<Actions>::operator testing::Action<R(Args ...)>() const [with R = void; Args = {std::function<void(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&&)>}; Actions = {testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]':
<source>:33:18: required from here
/opt/compiler-explorer/libs/googletest/trunk/googlemock/include/gmock/gmock-actions.h:1061:56: error: could not convert 'std::get<1, testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >(((const testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >*)this)->testing::internal::DoAllAction<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::actions)' from 'std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >' {aka 'const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'} to 'testing::Action<void(std::function<void(std::vector<std::__cxx11::basic_string<char> >&&)>)>'
1061 | std::get<sizeof...(Actions) - 1>(actions)};
| ^
| |
| std::__tuple_element_t<1, std::tuple<testing::PolymorphicAction<testing::internal::ReturnVoidAction>, testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > {aka const testing::internal::InvokeArgumentAction<0, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}
Execution build compiler returned: 1
If I remove the &&and change the argument to the function from a rvalue reference into a copy, it compiles fine. What could be happening here?
It seems it is unable to work with r-value references. The workaround:
EXPECT_CALL(myService, bar(testing::_)).WillOnce(
testing::WithArg<0>(
[] (std::function<void (std::vector<std::string>&&)> f) {
f({"hello", "world"});
}
)
Related issue has been closed for some reasons Add support for move only parameters in mocked methods.
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 am having a bit of trouble deep copying a pointer to a map of pointer values.
std::map<string, TH1D*>* m_hist_split;
My current method:
I am using a class template that copies the map pair and initializes the value of the pair on the heap. the template then returns a map::type_value
#ifndef DEEPCOPY_H
#define DEEPCOPY_H
#include <map>
#include <algorithm>
namespace DeepCopy{
template<class A, class B>
struct MapPointerValue{
typedef typename std::map< A, B* >::value_type map_t;
map_t operator() (map_t p){
return std::make_pair(p.first, new B(*p.second) );
}
};
...
...
}
#endif
Inside my copy constructor I then attempt to deep copy my pointer to a map of pointer values.
Cutflow::Cutflow(const Cutflow& cpy){
m_hist_split = new map<string, TH1D*>;
transform(cpy.m_hist_split->begin(),cpy.m_hist_split->end(), inserter(m_hist_split, m_hist_split->begin()), DeepCopy::MapPointerValue<string, TH1D>() );
...
...
}
When compiling I then get an ugly error :-( :
Compiling src/Cutflow.cxx to obj/Cutflow.o
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h: In instantiation of 'std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*>':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:562: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:572: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:599: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:608: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h: In function 'std::insert_iterator<_Container> std::inserter(_Container&, _Iterator) [with _Container = std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*, _Iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> >]':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:648: error: 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*' is not a class, struct, or union type
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h: In function '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*> >, _OIter = std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*>, _UnaryOperation = DeepCopy::MapPointerValue<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D>]':
src/Cutflow.cxx:42: instantiated from here
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h:4281: error: no match for 'operator=' in '__result.std::insert_iterator<_Container>::operator* [with _Container = std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*]() = DeepCopy::MapPointerValue<A, B>::operator()(typename std::map<A, B*, std::less<_Key>, std::allocator<std::pair<const A, B*> > >::value_type) [with A = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, B = TH1D](std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>(((const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>&)((const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>*)__first.std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*>]()))))'
x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/stl_iterator.h:559: note: candidates are: std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*>& std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*>::operator=(const std::insert_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TH1D*, 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> >, TH1D*> > >*>&)
Any ideas what I am doing wrong.
Also,
is what I am doing correct?
:-D
In the following line
transform(cpy.m_hist_split->begin(),cpy.m_hist_split->end(), inserter(m_hist_split, m_hist_split->begin()), DeepCopy::MapPointerValue<string, TH1D>() );
you have mistakenly called the inserter function with a pointer to a map as the first parameter instead of a reference.
For completeness, inserter is defined in §24.5.2.6.5 like this:
template <class Container>
insert_iterator<Container> inserter(Container& x, typename Container::iterator i);
Returns: insert_iterator(x, i)
In this case, a simple typo caused a big headache.
I have a graph class that employs two dimension vertices using a map with another map nested inside, as such(V is my template):
map< V , map< V , int> > vertices;
Defining an iterator as such seems to be ok:
typename map< V, map< V , int>::iterator i;
However when I try to use that iterator, like this i = vertices.begin() , I get a long error that looks like this. (main.cpp 81 is where I call the method that does the iteration). Where could I look at to find what's wrong?
main.cpp:81: instantiated from here
graph.h:326: error: no match for ‘operator=’ in ‘i = ((const GraphNameSpace::Graph<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)this)->GraphNameSpace::Graph<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::vertices.std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = 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> > >, _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::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> > > > >]()’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:152: note: candidates are: std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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> > > > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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> > > > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 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> > > > >&)
make: *** [main.o] Error 1
UPDATE:
At the risk of providing too much information, here is my graph class on pastebin. Right at the very bottom in the dump function is where I'm attempting to do the iteration.
The member function dump(), inside which you write i = vertices.begin(), is a const member function, which effectively makes vertices a const object, for it is a member of the class. So you need const_iterator:
typename map<V, map<V, int> >::const_iterator i;
//^^^^^ note this
It should work now. :-)
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 ;