vector of variants with forward declaration [duplicate] - c++

This question already has answers here:
std::map::reverse_iterator doesn't work with C++20 when used with incomplete type [duplicate]
(1 answer)
map with incomplete value type
(1 answer)
std::variant and incomplete type: how does it work?
(1 answer)
Closed 4 months ago.
I would like to understand why, using Visual Studio 2019 or 2022, the following code compiles with the a and b variables but not with the c variable. Is there any workaround for this issue?
#include <variant>
#include <vector>
struct data; // defined in another file
std::variant<data>* a; // ok
std::vector<data*> b; // ok
std::vector<std::variant<data>*> c; // error C2139: 'data': an undefined class
// is not allowed as an argument to
// compiler intrinsic type trait
// '__is_trivially_destructible'
The complete error message is:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(931,60): error C2139: 'data': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_trivially_destructible'
1>C:\Temp\test2019\main.cpp(4): message : see declaration of 'data'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(931): message : see reference to variable template 'const bool conjunction_v<std::is_trivially_destructible<data> >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(1006): message : see reference to alias template instantiation 'std::_Variant_destroy_layer<data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1667): message : see reference to class template instantiation 'std::variant<data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1665): message : while compiling class template member function 'void std::vector<std::variant<data> *,std::allocator<std::variant<data> *>>::_Destroy(std::variant<data> **,std::variant<data> **)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector(1751): message : see reference to function template instantiation 'void std::vector<std::variant<data> *,std::allocator<std::variant<data> *>>::_Destroy(std::variant<data> **,std::variant<data> **)' being compiled
1>C:\Temp\test2019\main.cpp(8): message : see reference to class template instantiation 'std::vector<std::variant<data> *,std::allocator<std::variant<data> *>>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(60,19): error C2139: 'data': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_constructible'
1>C:\Temp\test2019\main.cpp(4): message : see declaration of 'data'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(60): message : see reference to variable template 'const bool conjunction_v<std::is_move_constructible<data>,std::negation<std::conjunction<std::is_trivially_move_constructible<data> > > >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(97): message : see reference to alias template instantiation 'std::_SMF_control_move<std::_Variant_destroy_layer_<data>,data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(39,19): error C2139: 'data': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_constructible'
1>C:\Temp\test2019\main.cpp(4): message : see declaration of 'data'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(39): message : see reference to variable template 'const bool conjunction_v<std::is_copy_constructible<data>,std::negation<std::conjunction<std::is_trivially_copy_constructible<data> > > >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(61): message : see reference to alias template instantiation 'std::_SMF_control_copy<std::_Variant_destroy_layer_<data>,data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(462,32): error C2079: 'std::_Variant_storage_<false,data>::_Head' uses undefined struct 'data'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(810): message : see reference to class template instantiation 'std::_Variant_storage_<false,data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(916): message : see reference to class template instantiation 'std::_Variant_base<data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(82): message : see reference to class template instantiation 'std::_Variant_destroy_layer_<data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xsmf_control.h(120): message : see reference to class template instantiation 'std::_Deleted_copy_assign<std::_Variant_destroy_layer_<data>,data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(1006): message : see reference to class template instantiation 'std::_Deleted_move_assign<std::_Variant_destroy_layer_<data>,data>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(1008,86): error C2139: 'data': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_destructible'
1>C:\Temp\test2019\main.cpp(4): message : see declaration of 'data'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(1008): message : see reference to variable template 'const bool conjunction_v<std::is_object<data>,std::negation<std::is_array<data> >,std::is_destructible<data> >' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\variant(1008,19): error C2338: variant<Types...> requires all of the Types to meet the Cpp17Destructible requirements N4828 [variant.variant]/2.

Related

Invoking std::thread with class operator()() that has CWinThread as base fails

Background:
My project is C++ MFC. But I am writing the code using modern C++ as the standard evolves. Currently, I invoke threads sometimes using std::thread (later code) and sometimes the MFC CreateThread complete with required waitForSingleObject and thread priority parameter. Clearly, the std::thread is more elegant and therefore more maintainable. It is important to over time make the code consistent and drop old idioms where possible.
Problem:
But invoking std::thread with a class Baz in sample code below that has CWinThread as its base, fails to find a std::thread constructor.
Sample Code:
struct Caz {};
struct Baz : public CWinThread // works if base is Caz
{
void operator()() {}
};
std::shared_ptr< Baz > b;
b = std::make_shared< Baz >();
std::thread bazThread( *b ); // error: can't find a constructor when CWinThread is base, but can for Caz as base
When the base class is CWinThread it fails to resolve, but when the base class is Caz it works fine. Both Caz and CWinThread have default constructors.
Why is CWinThread making it fail?
Edit1:
The error message set is:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2057,35): error C2665: 'std::tuple::tuple': none of the 2 overloads could convert all the argument types
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\tuple(321,5): message : could be 'std::tuple::tuple(std::tuple &&)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\tuple(320,5): message : or 'std::tuple::tuple(const std::tuple &)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : while trying to match the argument list '(_Ty2)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : _Ty2=Baz
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : see reference to function template instantiation 'std::unique_ptr<_Tuple,std::default_delete<_Ty>> std::make_unique<_Tuple,_Ty2&,0>(_Ty2 &)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : _Ty=_Tuple,
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : _Ty2=Baz
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : ]
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : see reference to function template instantiation 'std::thread::thread<_Ty2&,,void>(_Fn)' being compiled
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : with
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : [
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : _Ty2=Baz,
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : _Fn=Baz &
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : ]
1>Done building project "SdcdcHiveware.vcxproj" -- FAILED.
Edit2:
// FUNCTION TEMPLATE make_unique
template <class _Ty, class... _Types, enable_if_t<!is_array_v<_Ty>, int> = 0>
_NODISCARD unique_ptr<_Ty> make_unique(_Types&&... _Args) { // make a unique_ptr
return unique_ptr<_Ty>(new _Ty(_STD forward<_Types>(_Args)...));
}
Tentative answer unless someone finds a workaround.
std::thread cannot be used in MFC projects for class types that inherit from CWinThread, also called user threads for which sample code may be found at Creating Threads.
For traditional windows worker threads invoking std::thread instead works fine.

Seeming error in Visual C++ 2017 ( ver 15.9.3 )

Have the following code which compiles and runs fine in Visual C++ 2013, and the C++14 standard on http://cpp.sh. However, on Visual C++ 2017 ( ver 15.9.3 ), it gives an error message... which is maybe a bug?
Code is:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
vector<long> v1 = {1}, v2 = {9};
swap<vector<long>>(v1, v2);
cout << "v1.front(): " << v1.front() << endl;
cout << "v2.front(): " << v2.front() << endl;
return 0;
}
I should note that if I comment out swap() function, then it compiles and runs ok. The error messages are all resulting from the call to swap().
Error messages are:
maptest.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2048): error C2039: '_Alloc': is not a member of 'std::vector<long,std::allocator<_Ty>>'
with
[
_Ty=long
]
maptest.cpp(9): note: see declaration of 'std::vector<long,std::allocator<_Ty>>'
with
[
_Ty=long
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2096): note: see reference to class template instantiation 'std::_Vb_iter_base<_Alvbase_wrapped>' being compiled
with
[
_Alvbase_wrapped=std::vector<long,std::allocator<long>>
]
maptest.cpp(11): note: see reference to class template instantiation 'std::_Vb_reference<std::vector<long,std::allocator<_Ty>>>' being compiled
with
[
_Ty=long
]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2048): error C2061: syntax error: identifier '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2923: 'std::allocator_traits': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2955: 'std::allocator_traits': use of class template requires template argument list
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xmemory0(903): note: see declaration of 'std::allocator_traits'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2039: 'size_type': is not a member of 'std::allocator_traits<_Alloc>'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2049): error C2061: syntax error: identifier 'size_type'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2923: 'std::allocator_traits': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2955: 'std::allocator_traits': use of class template requires template argument list
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xmemory0(903): note: see declaration of 'std::allocator_traits'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2039: 'difference_type': is not a member of 'std::allocator_traits<_Alloc>'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2050): error C2061: syntax error: identifier 'difference_type'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2051): error C2065: '_Alvbase': undeclared identifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2051): error C2923: 'std::_Rebind_alloc_t': '_Alvbase' is not a valid template type argument for parameter '_Alloc'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2058): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2065): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2072): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2089): error C3646: '_Myoff': unknown override specifier
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector(2089): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
std::swap is overloaded for std::vector as
template< class T, class Alloc>
void swap(vector<T,Alloc>& lhs, vector<T,Alloc>& rhs);
The correct template arguments for your given vector would be T=long, Alloc = std::allocator<long>. The proper way to call swap (or almost any other templated function) on an std::vector is to simply drop the explicit template argument specification altogether and let template argument deduction do its work.

Conversion Error from 'unsigned __int64' to 'unsigned __int64 *' - Pybind11

After downloading this https://github.com/pybind/pybind11/archive/v2.2.3.zip
and creating a simple cpp file:
#include <pybind11/pybind11.h>
int add(int i, int j) { return i + j; }
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
}
with
I get this error
Error C2446 '<': no conversion from 'unsigned __int64' to 'unsigned __int64 *' in c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector line 2326
Would anybody know what this could be referring to, and possibly how to fix it?
As requested, here the full error log:
1>------ Rebuild All started: Project: Test_CreatePythonBindings, Configuration: Debug x64 ------
1>example.cpp
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2326): error C2446: '<': no conversion from 'unsigned __int64' to 'unsigned __int64 *'
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2326): note: Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2325): note: while compiling class template member function 'std::_Vb_const_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>> &std::_Vb_const_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>>::operator +=(__int64)'
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2545): note: see reference to function template instantiation 'std::_Vb_const_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>> &std::_Vb_const_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>>::operator +=(__int64)' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2490): note: see reference to class template instantiation 'std::_Vb_const_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(3094): note: see reference to class template instantiation 'std::_Vb_iterator<std::_Wrap_alloc<std::allocator<std::_Vbase>>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(3093): note: while compiling class template member function 'void std::vector<bool,std::allocator<_Ty>>::push_back(const bool &)'
1> with
1> [
1> _Ty=bool
1> ]
1>c:\pybind11-2.2.3\include\pybind11\pybind11.h(513): note: see reference to function template instantiation 'void std::vector<bool,std::allocator<_Ty>>::push_back(const bool &)' being compiled
1> with
1> [
1> _Ty=bool
1> ]
1>c:\pybind11-2.2.3\include\pybind11\cast.h(1806): note: see reference to class template instantiation 'std::vector<bool,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=bool
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\vector(2326): warning C4554: '&': check operator precedence for possible error; use parentheses to clarify precedence
1>Done building project "Test_CreatePythonBindings.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
It appears to be a VS 2017 issue related to
std::vector<bool>
Separate question asked here:
Pushback on vector<bool> fails - VS 2017

Unexpected template instantiation leading to a compile error

I just wonder why my code cannot be compiled. Is the below okay? I'm trying to declare a simple class with Category1 and Category2 typedef-s.
Category1 typedef compiles fine but Category2 one does not.
It seems that Category2 typedef cannot be compiled because class iterator_traits<> is instantiated despite the surrounding class X is not instantiated... Seems very confusing for me.
#include <iterator>
template <class GetterFunType>
struct X {
GetterFunType containerGetterFun;
// works
typedef typename std::iterator_traits<typename GetterFunType::iterator>::iterator_category Category1;
// compile error - std::iterator_traits<> is instantiated with type 'unknown'
typedef typename std::iterator_traits<
decltype(containerGetterFun().begin())>::iterator_category Category2;
X(GetterFunType _containerGetterFun) : containerGetterFun(_containerGetterFun) { }
};
Note that I do not need to instantiate class X to get the following errors (the above is the complete compilation unit).
In Visual Studio 2012 I'm getting this:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(11) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=unknown
1> ]
1> c:\data\fsl\apif_src_review\apif_src\systemns.cpp(14) : see reference to class template instantiation 'X<GetterFunType>' being compiled
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C3254: 'std::iterator_traits<_Iter>' : class contains explicit override 'iterator_category' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2838: 'iterator_category' : illegal qualified name in member declaration
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=unknown
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=unknown
1> ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=unknown
1> ]
In xutility(364) there is:
template<class _Iter>
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type;
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
My case is that I want to declare a class that gets a lambda in constructor. The lambda is expected to return a reference to a container. And I need to determine wheter the returned container has a random-access iterator. But I got stuck with this compilation error. Thank you for explanation!
I was able to compile the same code without any errors using gcc 5.3.1, with -std=c++11
Your compiler is a relatively old compiler that does not support the current C++1x standard. Switching to another compiler is the only option I can see here, if you need to use modern C++ features.
I see that the code is okay but there are some Visual Studio 2012 limitations not allowing to compile. At least in MSVS 2015 it works, same as for gcc 5.3.1. Thanks, friends!

implementation of hash table [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 12 years ago.
i have following code
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <hash_map>
#include <string>
#include <iterator>
#include <ostream>
using namespace std;
struct Equal
{
bool operator()(const char *s1,const char *s2)const
{
return std::strcmp(s1,s2)==0;
}
};
typedef std::hash_multimap<const char*,int,hash<const char*>,Equal>map_type;
void lookup(const map_type&Map,const char *str){
cout<<str<<":";
pair<map_type::const_iterator ,map_type::const_iterator>p=Map.equal_range(str);
for (map_type::const_iterator i=p.first;i!=p.second;++i)
cout << (*i).second<<" ";
}
int maain(){
map_type m;
m.insert(map_type::value_type("H", 1));
m.insert(map_type::value_type("H", 2));
m.insert(map_type::value_type("C", 12));
m.insert(map_type::value_type("C", 13));
m.insert(map_type::value_type("O", 16));
m.insert(map_type::value_type("O", 17));
m.insert(map_type::value_type("O", 18));
m.insert(map_type::value_type("I", 127));
lookup(M,"I");
lookup(M,"0");
lookup(M,"Rn");
return 0;
}
but here is errors
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
1> c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\hash_map(273) : see reference to class template instantiation 'std::_Hash<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(22) : see reference to class template instantiation 'stdext::hash_multimap<_Kty,_Ty,_Tr,_Alloc>' being compiled
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'rebind' : is not a member of 'Equal'
1> c:\users\7\documents\visual studio 2010\projects\hash_tables\hash_tables\hash_tables.cpp(11) : see declaration of 'Equal'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2143: syntax error : missing ';' before '<'
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2039: 'other' : is not a member of '`global namespace''
1>c:\program files\microsoft visual studio 10.0\vc\include\hash_map(27): error C2238: unexpected token(s) preceding ';'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2039: 'bucket_size' : is not a member of 'std::hash<_Kty>'
1> with
1> [
1> _Kty=const char *
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(180): error C2065: 'bucket_size' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2039: 'allocator_type' : is not a member of 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>'
1> with
1> [
1> _Kty=const char *,
1> _Ty=int,
1> _Tr=std::hash<const char *>,
1> _Alloc=Equal,
1> _Mfl=true
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2146: syntax error : missing ',' before identifier 'allocator_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(184): error C2065: 'allocator_type' : undeclared identifier
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(186): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(187): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(188): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(189): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(190): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(191): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(192): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(193): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(197): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2955: 'std::list' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 10.0\vc\include\list(579) : see declaration of 'std::list'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2146: syntax error : missing ';' before identifier 'iterator'
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C3254: 'std::_Hash<_Traits>' : class contains explicit override 'type' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Traits=std::_Hmap_traits<const char *,int,std::hash<const char *>,Equal,true>
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2838: 'type' : illegal qualified name in member declaration
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): error C2208: 'std::iterator' : no members defined using this type
1>c:\program files\microsoft visual studio 10.0\vc\include\xhash(198): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.96
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
please help
You have to look at each complier error and fix them one at a time. We can't take a file of code and fix each error. Learning to live and even on occasion love the abusive spouse that is your complier is part of the process.
All your errors seem related to the fact that you're not passing the right template arguments to hash_multimap. Take a look at hash_multimap's documentation. The last parameter, Ăˆqual` should be the allocator, not a function object, check out the 1st error:
c:\program files\microsoft visual studio 10.0\vc\include\hash_map(26): error C2903: 'rebind' : symbol is neither a class template nor a function template
c:\program files\microsoft visual studio 10.0\vc\include\xhash(170) : see reference to class template instantiation 'std::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
with
[
_Kty=const char *,
_Ty=int,
_Tr=std::hash,
_Alloc=Equal,
_Mfl=true
]
Edit:
If you've looked at SGI's hash_multimap documentation, you'll notice that the hash_map and hash_multimap classes are different between vendors because they are not standard. The next version of the standard comprises standard hashed containers. In the mean time, well, you'll have to write non portable code!