Related
In my project I have the following piece of code, which works perfectly fine:
#include <optional>
#include <ostream>
using optional_stream_ref = std::optional<std::reference_wrapper<std::ostream>>;
which is then used in functions such as this:
template<typename State, typename Matrix>
inline auto state_energy(const State& state, Matrix& matrix, optional_stream_ref out = std::nullopt) -> typename State::energy_t
{
typename State::energy_t energy = state.calc_energy(matrix, out);
return energy;
}
This compiles and works.
But for some reason when I try to do it in vacuum, just testing, it doesn't compile.
This is main.cpp example:
#include<optional>
#include<ostream>
using optional_stream_ref = std::optional<std::reference_wrapper<std::ostream>>;
void f(int a, optional_stream_ref out = std::nullopt)
{
}
int main()
{
}
which gives this compilation error:
g++ -std=c++17 -O3 -pedantic -Wall -Wextra -Ilib/ -DNDEBUG main.cpp
In file included from /usr/include/c++/9/bits/move.h:55,
from /usr/include/c++/9/bits/nested_exception.h:40,
from /usr/include/c++/9/exception:144,
from /usr/include/c++/9/ios:39,
from /usr/include/c++/9/ostream:38,
from main.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::__is_trivially_copy_constructible_impl<std::reference_wrapper<std::basic_ostream<char> >, true>’:
/usr/include/c++/9/type_traits:1164:12: required from ‘struct std::is_trivially_copy_constructible<std::reference_wrapper<std::basic_ostream<char> > >’
/usr/include/c++/9/type_traits:2945:25: required from ‘constexpr const bool std::is_trivially_copy_constructible_v<std::reference_wrapper<std::basic_ostream<char> > >’
/usr/include/c++/9/optional:656:11: required from ‘class std::optional<std::reference_wrapper<std::basic_ostream<char> > >’
main.cpp:7:60: required from here
/usr/include/c++/9/type_traits:1157:12: error: invalid use of incomplete type ‘class std::reference_wrapper<std::basic_ostream<char> >’
1157 | struct __is_trivially_copy_constructible_impl<_Tp, true>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits:2020:11: note: declaration of ‘class std::reference_wrapper<std::basic_ostream<char> >’
2020 | class reference_wrapper;
| ^~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_copy_constructible_v<std::reference_wrapper<std::basic_ostream<char> > >’:
/usr/include/c++/9/optional:656:11: required from ‘class std::optional<std::reference_wrapper<std::basic_ostream<char> > >’
main.cpp:7:60: required from here
/usr/include/c++/9/type_traits:2945:25: error: ‘value’ is not a member of ‘std::is_trivially_copy_constructible<std::reference_wrapper<std::basic_ostream<char> > >’
2945 | inline constexpr bool is_trivially_copy_constructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::__is_trivially_move_constructible_impl<std::reference_wrapper<std::basic_ostream<char> >, true>’:
/usr/include/c++/9/type_traits:1185:12: required from ‘struct std::is_trivially_move_constructible<std::reference_wrapper<std::basic_ostream<char> > >’
/usr/include/c++/9/type_traits:2948:25: required from ‘constexpr const bool std::is_trivially_move_constructible_v<std::reference_wrapper<std::basic_ostream<char> > >’
/usr/include/c++/9/optional:656:11: required from ‘class std::optional<std::reference_wrapper<std::basic_ostream<char> > >’
main.cpp:7:60: required from here
/usr/include/c++/9/type_traits:1178:12: error: invalid use of incomplete type ‘class std::reference_wrapper<std::basic_ostream<char> >’
1178 | struct __is_trivially_move_constructible_impl<_Tp, true>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits:2020:11: note: declaration of ‘class std::reference_wrapper<std::basic_ostream<char> >’
2020 | class reference_wrapper;
| ^~~~~~~~~~~~~~~~~
/usr/include/c++/9/type_traits: In instantiation of ‘constexpr const bool std::is_trivially_move_constructible_v<std::reference_wrapper<std::basic_ostream<char> > >’:
/usr/include/c++/9/optional:656:11: required from ‘class std::optional<std::reference_wrapper<std::basic_ostream<char> > >’
main.cpp:7:60: required from here
/usr/include/c++/9/type_traits:2948:25: error: ‘value’ is not a member of ‘std::is_trivially_move_constructible<std::reference_wrapper<std::basic_ostream<char> > >’
2948 | inline constexpr bool is_trivially_move_constructible_v =
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why can it compile in one place, but fail in another?
Where is the error?
Thanks!
#include <functional> to get the canonical definition of std::reference_wrapper.
As a general rule, cppreference includes the #include required for each std library template and type. So I just google it ("reference wrapper cppreference", misspellings ok) and look at the top of the page.
C++ std header files are free to include other std header files, types and template, or forward declare them. But if you want to reliably and portably use a template or type from std, you need to #include the correct header.
To diagnose this problem yourself from an error-stream, first look for the first error in it:
/usr/include/c++/9/type_traits:1157:12: error: invalid use of incomplete type ‘class std::reference_wrapper<std::basic_ostream<char> >’
It states std::reference_wrapper is incomplete. Then you can try to work out why this error is generated either from that one line, or you can follow the "breadcrumbs" of how you got there. Here, simply looking at that error line makes the solution relatively obvious; find a way to make std::reference_wrapper complete.
(I had seen this problem before, so I just assumed the std construct you didn't obviously #include a header for was missing, without having to look at your error messages.)
When I declare a variable of std::unordered_map<boost::any, boost::any> type, it throws annoying compile errors.
For an example, any.cc:
#include <map>
#include <boost/any.hpp>
int main() {
std::map<boost::any, boost::any> dict;
return 0;
}
Compiling above code as g++ any.cc -std=c++11 -I/usr/include/boost occurs a lot of errors like following:
In file included from /usr/include/c++/5/bits/hashtable.h:35:0,
from /usr/include/c++/5/unordered_map:47,
from any.cc:1:
/usr/include/c++/5/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> >’:
/usr/include/c++/5/type_traits:137:12: required from ‘struct std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > >’
/usr/include/c++/5/type_traits:148:38: required from ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
/usr/include/c++/5/bits/unordered_map.h:100:66: required from ‘class std::unordered_map<boost::any, boost::any>’
any.cc:5:48: required from here
/usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match for call to ‘(const std::hash<boost::any>) (const boost::any&)’
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
^
In file included from /usr/include/c++/5/bits/move.h:57:0,
from /usr/include/c++/5/bits/stl_pair.h:59,
from /usr/include/c++/5/utility:70,
from /usr/include/c++/5/unordered_map:38,
from any.cc:1:
/usr/include/c++/5/type_traits: In instantiation of ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’:
/usr/include/c++/5/bits/unordered_map.h:100:66: required from ‘class std::unordered_map<boost::any, boost::any>’
any.cc:5:48: required from here
/usr/include/c++/5/type_traits:148:38: error: ‘value’ is not a member of ‘std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > >’
: public integral_constant<bool, !_Pp::value>
^
In file included from /usr/include/c++/5/unordered_map:48:0,
from any.cc:1:
/usr/include/c++/5/bits/unordered_map.h: In instantiation of ‘class std::unordered_map<boost::any, boost::any>’:
any.cc:5:48: required from here
/usr/include/c++/5/bits/unordered_map.h:100:66: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
^
/usr/include/c++/5/bits/unordered_map.h:107:45: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::key_type key_type;
^
/usr/include/c++/5/bits/unordered_map.h:108:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::value_type value_type;
^
/usr/include/c++/5/bits/unordered_map.h:109:48: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::mapped_type mapped_type;
^
/usr/include/c++/5/bits/unordered_map.h:110:43: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::hasher hasher;
^
/usr/include/c++/5/bits/unordered_map.h:111:46: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::key_equal key_equal;
^
/usr/include/c++/5/bits/unordered_map.h:112:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::allocator_type allocator_type;
^
/usr/include/c++/5/bits/unordered_map.h:117:45: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::pointer pointer;
^
/usr/include/c++/5/bits/unordered_map.h:118:50: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::const_pointer const_pointer;
^
/usr/include/c++/5/bits/unordered_map.h:119:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::reference reference;
^
/usr/include/c++/5/bits/unordered_map.h:120:52: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::const_reference const_reference;
^
/usr/include/c++/5/bits/unordered_map.h:121:46: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::iterator iterator;
^
/usr/include/c++/5/bits/unordered_map.h:122:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::const_iterator const_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:123:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::local_iterator local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:124:57: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::const_local_iterator const_local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:125:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::size_type size_type;
^
/usr/include/c++/5/bits/unordered_map.h:126:52: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
typedef typename _Hashtable::difference_type difference_type;
^
/usr/include/c++/5/bits/unordered_map.h:280:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
operator=(initializer_list<value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:379:2: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
emplace(_Args&&... __args)
^
/usr/include/c++/5/bits/unordered_map.h:432:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
insert(const value_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:439:2: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
insert(_Pair&& __x)
^
/usr/include/c++/5/bits/unordered_map.h:499:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
insert(initializer_list<value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:645:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
equal_range(const key_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:649:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’
equal_range(const key_type& __x) const
^
But when I use boost::unordered_map rather than std, it works without any errors.
Why?
std::unordered_map uses std::hash as default hasher. There is no specialization of std::hash for boost::any. Because STanDard library does not know anything about entity from 3rd party library.
boost::unordered_map uses boost::hash as default hasher, which has specialization for types from boost library.
Solution: explicitely tell std::unordered_map to use boost::hash<boost::any> as hasher.
When you declare an unordered_map<Key, Value>, you need a hashing function. The default hashing function is const hash<Key>(const Key&).
boost::any, it appears, has such hashing template specialization, but for boost::hash.
So in effect the compiler tries to find a specialization for std::hash for boost::any, but finds only boost::hash specialization.
I am trying to learn how boost filesystem work, but I can't get through some compilation problems
#include <boost/filesystem.hpp>
#include <iostream>
using namespace boost::filesystem;
int main()
{
path p = current_path();
directory_iterator it{p};
while (it != directory_iterator{})
std::cout << *it++ << '\n';
}
this is how I try to compile.
My g++ version is 4.7
g++ -std=c++11 filesystem.cc -lboost_filesystem -o filesystem
After this I get thousands of errors.
For example
In file included from /usr/include/boost/iterator/interoperable.hpp:13:0,
from /usr/include/boost/iterator/iterator_facade.hpp:11,
from /usr/include/boost/filesystem/path.hpp:22,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/type_traits/is_convertible.hpp: In instantiation of ‘const bool boost::detail::is_convertible_basic_impl<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::value’:
/usr/include/boost/type_traits/is_convertible.hpp:295:5: required from ‘const bool boost::detail::is_convertible_impl<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::value’
/usr/include/boost/type_traits/is_convertible.hpp:418:1: required from ‘struct boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >’
/usr/include/boost/mpl/aux_/nested_type_wknd.hpp:26:31: required from ‘struct boost::mpl::aux::nested_type_wknd<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > > >’
/usr/include/boost/mpl/aux_/preprocessed/gcc/or.hpp:48:8: required from ‘struct boost::mpl::or_<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >, boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >’
/usr/include/boost/iterator/detail/enable_if.hpp:68:12: required from ‘struct boost::iterators::enable_if<boost::mpl::or_<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >, boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >, bool>’
/usr/include/boost/iterator/iterator_facade.hpp:67:12: required from ‘struct boost::detail::enable_if_interoperable<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >, bool>’
/usr/include/boost/iterator/iterator_facade.hpp:837:3: required by substitution of ‘template<class Derived1, class V1, class TC1, class Reference1, class Difference1, class Derived2, class V2, class TC2, class Reference2, class Difference2> typename boost::detail::enable_if_interoperable<Derived1, Derived2, typename boost::mpl::apply2<boost::detail::always_bool2, Derived1, Derived2>::type>::type boost::operator!=(const boost::iterator_facade<Derived1, V1, TC1, Reference1, Difference1>&, const boost::iterator_facade<Derived2, V2, TC2, Reference2, Difference2>&) [with Derived1 = boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >; V1 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >; TC1 = boost::single_pass_traversal_tag; Reference1 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&; Difference1 = long int; Derived2 = boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >; V2 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >; TC2 = boost::single_pass_traversal_tag; Reference2 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&; Difference2 = long int]’
filesystem.cc:10:35: required from here
/usr/include/boost/type_traits/is_convertible.hpp:136:49: error: use of deleted function ‘boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::basic_directory_iterator(const boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&)’
In file included from /usr/include/boost/filesystem.hpp:16:0,
from filesystem.cc:1:
/usr/include/boost/filesystem/operations.hpp:891:11: note: ‘boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::basic_directory_iterator(const boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/filesystem/operations.hpp:891:11: error: use of deleted function ‘boost::shared_ptr<boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::shared_ptr(const boost::shared_ptr<boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >&)’
In file included from /usr/include/boost/shared_ptr.hpp:17:0,
from /usr/include/boost/filesystem/path.hpp:24,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/smart_ptr/shared_ptr.hpp:168:25: note: ‘boost::shared_ptr<boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::shared_ptr(const boost::shared_ptr<boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >&)’ is implicitly declared as deleted because ‘boost::shared_ptr<boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >’ declares a move constructor or move assignment operator
In file included from /usr/include/boost/iterator/interoperable.hpp:13:0,
from /usr/include/boost/iterator/iterator_facade.hpp:11,
from /usr/include/boost/filesystem/path.hpp:22,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/type_traits/is_convertible.hpp:128:41: error: initializing argument 1 of ‘static boost::type_traits::yes_type boost::detail::checker<T>::_m_check(T, int) [with T = boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >; boost::type_traits::yes_type = char]’
In file included from /usr/include/boost/filesystem/path.hpp:23:0,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/throw_exception.hpp: In instantiation of ‘void boost::throw_exception(const E&) [with E = boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >]’:
/usr/include/boost/filesystem/operations.hpp:280:9: required from ‘typename boost::enable_if<boost::filesystem::is_basic_path<Path>, boost::filesystem::file_status>::type boost::filesystem::symlink_status(const Path&) [with Path = boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits>; typename boost::enable_if<boost::filesystem::is_basic_path<Path>, boost::filesystem::file_status>::type = boost::filesystem::file_status]’
/usr/include/boost/filesystem/operations.hpp:287:45: required from here
/usr/include/boost/throw_exception.hpp:65:56: error: use of deleted function ‘boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > > >::clone_impl(const boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > > >&)’
In file included from /usr/include/boost/throw_exception.hpp:37:0,
from /usr/include/boost/filesystem/path.hpp:23,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/exception/exception.hpp:377:9: note: ‘boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > > >::clone_impl(const boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > > >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/exception/exception.hpp:377:9: error: use of deleted function ‘boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::error_info_injector(const boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >&)’
/usr/include/boost/exception/exception.hpp:287:9: note: ‘boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >::error_info_injector(const boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> > >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/exception/exception.hpp:287:9: error: use of deleted function ‘boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::basic_filesystem_error(const boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&)’
In file included from /usr/include/boost/filesystem/operations.hpp:17:0,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/filesystem/path.hpp:681:11: note: ‘boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::basic_filesystem_error(const boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/filesystem/path.hpp:681:11: error: use of deleted function ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::m_imp>::shared_ptr(const boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::m_imp>&)’
In file included from /usr/include/boost/shared_ptr.hpp:17:0,
from /usr/include/boost/filesystem/path.hpp:24,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/smart_ptr/shared_ptr.hpp:168:25: note: ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::m_imp>::shared_ptr(const boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::m_imp>&)’ is implicitly declared as deleted because ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<char>, boost::filesystem::path_traits> >::m_imp>’ declares a move constructor or move assignment operator
In file included from /usr/include/boost/filesystem/path.hpp:23:0,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/throw_exception.hpp: In instantiation of ‘void boost::throw_exception(const E&) [with E = boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >]’:
/usr/include/boost/filesystem/operations.hpp:261:9: required from ‘typename boost::enable_if<boost::filesystem::is_basic_path<Path>, boost::filesystem::file_status>::type boost::filesystem::status(const Path&) [with Path = boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits>; typename boost::enable_if<boost::filesystem::is_basic_path<Path>, boost::filesystem::file_status>::type = boost::filesystem::file_status]’
/usr/include/boost/filesystem/operations.hpp:620:34: required from here
/usr/include/boost/throw_exception.hpp:65:56: error: use of deleted function ‘boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > > >::clone_impl(const boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > > >&)’
In file included from /usr/include/boost/throw_exception.hpp:37:0,
from /usr/include/boost/filesystem/path.hpp:23,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/exception/exception.hpp:377:9: note: ‘boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > > >::clone_impl(const boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > > >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/exception/exception.hpp:377:9: error: use of deleted function ‘boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >::error_info_injector(const boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >&)’
/usr/include/boost/exception/exception.hpp:287:9: note: ‘boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >::error_info_injector(const boost::exception_detail::error_info_injector<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/exception/exception.hpp:287:9: error: use of deleted function ‘boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::basic_filesystem_error(const boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >&)’
In file included from /usr/include/boost/filesystem/operations.hpp:17:0,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/filesystem/path.hpp:681:11: note: ‘boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::basic_filesystem_error(const boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/boost/filesystem/path.hpp:681:11: error: use of deleted function ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::m_imp>::shared_ptr(const boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::m_imp>&)’
In file included from /usr/include/boost/shared_ptr.hpp:17:0,
from /usr/include/boost/filesystem/path.hpp:24,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/smart_ptr/shared_ptr.hpp:168:25: note: ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::m_imp>::shared_ptr(const boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::m_imp>&)’ is implicitly declared as deleted because ‘boost::shared_ptr<boost::filesystem::basic_filesystem_error<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >::m_imp>’ declares a move constructor or move assignment operator
In file included from /usr/include/boost/iterator/interoperable.hpp:13:0,
from /usr/include/boost/iterator/iterator_facade.hpp:11,
from /usr/include/boost/filesystem/path.hpp:22,
from /usr/include/boost/filesystem/operations.hpp:17,
from /usr/include/boost/filesystem.hpp:16,
from filesystem.cc:1:
/usr/include/boost/type_traits/is_convertible.hpp: In instantiation of ‘const bool boost::detail::is_convertible_basic_impl<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >&, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >::value’:
/usr/include/boost/type_traits/is_convertible.hpp:295:5: required from ‘const bool boost::detail::is_convertible_impl<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >::value’
/usr/include/boost/type_traits/is_convertible.hpp:418:1: required from ‘struct boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >’
/usr/include/boost/mpl/aux_/nested_type_wknd.hpp:26:31: required from ‘struct boost::mpl::aux::nested_type_wknd<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > > >’
/usr/include/boost/mpl/aux_/preprocessed/gcc/or.hpp:48:8: required from ‘struct boost::mpl::or_<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >, boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >’
/usr/include/boost/iterator/detail/enable_if.hpp:68:12: required from ‘struct boost::iterators::enable_if<boost::mpl::or_<boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >, boost::is_convertible<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> > >, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >, bool>’
/usr/include/boost/iterator/iterator_facade.hpp:67:12: required from ‘struct boost::detail::enable_if_interoperable<boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >, bool>’
/usr/include/boost/iterator/iterator_facade.hpp:837:3: required by substitution of ‘template<class Derived1, class V1, class TC1, class Reference1, class Difference1, class Derived2, class V2, class TC2, class Reference2, class Difference2> typename boost::detail::enable_if_interoperable<Derived1, Derived2, typename boost::mpl::apply2<boost::detail::always_bool2, Derived1, Derived2>::type>::type boost::operator!=(const boost::iterator_facade<Derived1, V1, TC1, Reference1, Difference1>&, const boost::iterator_facade<Derived2, V2, TC2, Reference2,
Difference2>&) [with Derived1 =
further part
boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >; V1 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >; TC1 = boost::single_pass_traversal_tag; Reference1 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >&; Difference1 = long int; Derived2 = boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >; V2 = boost::filesystem::basic_directory_entry<boost::filesystem::basic_path<std::basic_string<wchar_t>, boost::filesystem::wpath_traits> >; TC2 = boost::single_pass_traversal_tag; Reference2 =
#include <string>
#include <map>
#include <unordered_map>
using namespace std;
class Solution {
public:
private:
// unordered_map<string, int> mapStrInt; // Case 1: OK
// unordered_map<const string, int> mapStrInt; // Case 2: Fail
// map<string, int> mapStrInt; // Case 3: OK
// map<const string, int> mapStrInt; // Case 4: OK
};
Question> Why Case 2 is not legal?
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
> class unordered_map;
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
Based on http://www.compileonline.com/compile_cpp11_online.php
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::_Hash_code_base<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::__detail::_Select1st, std::hash<const std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>':
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1402:10: required from 'struct std::__detail::_Hashtable_base<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::__detail::_Select1st, std::equal_to<const std::basic_string<char> >, std::hash<const std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >'
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:174:11: required from 'class std::_Hashtable<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::allocator<std::pair<const std::basic_string<char>, int> >, std::__detail::_Select1st, std::equal_to<const std::basic_string<char> >, std::hash<const 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/local/gcc-4.8.1/include/c++/4.8.1/bits/unordered_map.h:100:18: required from 'class std::unordered_map<const std::basic_string<char>, int>'
main.cpp:11:38: required from here
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
== Updated Working code based on the comments ==
struct HashConstString
{
long operator()(const string& str) const {
return hash<string>()(str);
}
};
class Solution {
public:
private:
unordered_map<const string, int, HashConstString> mapStrInt; // Case 2: Now it works
};
From 21.6 we learn that four string-related hash functions are provided by the language:
template <> struct hash<string>;
template <> struct hash<u16string>;
template <> struct hash<u32string>;
template <> struct hash<wstring>;
Then from 23.5.2 we learn that the default hash for unordered_map is hash<Key> or in this case hash<const std::string>. Previously we learned that the implementation is not required to provide such a hash, so your code is not guaranteed to compile.
template <class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;
You can either use std::string as your key OR specify the hash function as hash<std::string> instead of relying on the default template parameter.
I have created a non-copyable map which I cannot get to compile with clang. Since clang is meant to be very standards compliant I was wondering if my code was legal. MSVS 2010 and GCC 4.7 compile this code without warnings or errors.
Full code is attached: the problematic line is the last line of main.
= delete needs removing for MSVS 2010
#include <utility>
#include <iostream>
#include <map>
template<typename Key_t, typename Value_t, typename Compare_t = std::less<Key_t> >
class non_copyable_map : public std::map<Key_t,Value_t,Compare_t>
{
typedef std::map<Key_t,Value_t,Compare_t> BaseType;
public:
non_copyable_map() { }
non_copyable_map(non_copyable_map&& t) : BaseType(std::move(t)) {}
non_copyable_map& operator = (non_copyable_map&& t)
{
if ( this != &t )
{
std::swap<BaseType>(*this,t);
}
return *this;
}
private:
non_copyable_map(const non_copyable_map&) = delete;
non_copyable_map& operator = (const non_copyable_map&) = delete;
};
int main(int argc, char* argv[])
{
non_copyable_map<int, non_copyable_map<int, int> > nestedMap;
non_copyable_map<int,int> inner;
inner[3]=4;
nestedMap[2] = std::move(inner); // THIS LINE CAUSES CLANG PROBLEMS
}
Error message when using clang++-mp-3.1 -std=c++0x -stdlib=libc++ MapOfMaps.cpp is:
In file included from MapOfMaps.cpp:2:
In file included from /usr/include/c++/v1/iostream:40:
In file included from /usr/include/c++/v1/istream:156:
In file included from /usr/include/c++/v1/ostream:134:
In file included from /usr/include/c++/v1/bitset:118:
/usr/include/c++/v1/__bit_reference:26:26: error: no type named '__storage_type' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
typedef typename _C::__storage_type __storage_type;
~~~~~~~~~~~~~^~~~~~~~~~~~~~
MapOfMaps.cpp:21:25: note: in instantiation of template class
'std::__1::__bit_reference<std::__1::map<int, int, std::__1::less<int>,
std::__1::allocator<std::__1::pair<const int, int> > > >' requested here
std::swap<BaseType>(*this,t);
^
MapOfMaps.cpp:36:15: note: in instantiation of member function 'non_copyable_map<int, int,
std::__1::less<int> >::operator=' requested here
nestedMap[2] = std::move(inner);
^
In file included from MapOfMaps.cpp:2:
In file included from /usr/include/c++/v1/iostream:40:
In file included from /usr/include/c++/v1/istream:156:
In file included from /usr/include/c++/v1/ostream:134:
In file included from /usr/include/c++/v1/bitset:118:
/usr/include/c++/v1/__bit_reference:27:26: error: no type named '__storage_pointer' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
typedef typename _C::__storage_pointer __storage_pointer;
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__bit_reference:33:25: error: no type named '__self' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
friend typename _C::__self;
~~~~~~~~~~~~~^~~~~~
In file included from MapOfMaps.cpp:3:
In file included from /usr/include/c++/v1/map:338:
/usr/include/c++/v1/__tree:1291:14: error: overload resolution selected deleted operator '='
__pair3_ = _STD::move(__t.__pair3_);
~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__tree:1308:9: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> >
>::__move_assign' requested here
__move_assign(__t, true_type());
^
/usr/include/c++/v1/__tree:1353:5: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> >
>::__move_assign' requested here
__move_assign(__t, integral_constant<bool,
^
/usr/include/c++/v1/map:736:21: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> > >::operator='
requested here
__tree_ = _STD::move(__m.__tree_);
^
/usr/include/c++/v1/type_traits:2342:9: note: in instantiation of member function
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >::operator=' requested here
__x = _STD::move(__y);
^
MapOfMaps.cpp:21:5: note: in instantiation of function template specialization
'std::__1::swap<std::__1::map<int, int, std::__1::less<int>,
std::__1::allocator<std::__1::pair<const int, int> > > >' requested here
std::swap<BaseType>(*this,t);
^
MapOfMaps.cpp:36:15: note: in instantiation of member function 'non_copyable_map<int, int,
std::__1::less<int> >::operator=' requested here
nestedMap[2] = std::move(inner);
^
/usr/include/c++/v1/memory:1918:7: note: candidate function (the implicit copy assignment
operator) has been explicitly deleted
class __compressed_pair
^
4 errors generated.
This may be a clang or libc++ bug (most likely libc++). I'm not duplicating your symptom with tip-of-trunk tools. The latest libc++ headers are here. This is a bit of a shot in the dark, but try this:
swap(static_cast<BaseType&>(*this),static_cast<BaseType&>(t));
It looks like you're getting tangled up with this swap in <__bit_reference>:
template <class _Cp, class _Dp>
_LIBCPP_INLINE_VISIBILITY inline
void
swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
which has nothing to do with maps. It just happens to be in scope.