Boost Optional with Boost Thread compilation issue - c++

My environment is Visual Stuido 2013, VC12, Boost 1.59.
The following code (a minimal repro of the real code):
#include "boost/thread.hpp"
#include "boost/optional.hpp"
class MyClass
{
public:
template <typename T>
operator const T& () const;
};
boost::optional<MyClass> foo()
{
boost::optional<MyClass> res;
return res;
}
int main(int argc)
{
foo();
}
Doesn't compile, the error:
1>------ Build started: Project: TestBoostOptional, Configuration: Debug x64 ------
1> main.cpp
1>c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(297): error C2664: 'void boost::optional_detail::optional_base::construct(MyClass &&)' : cannot convert argument 1 from 'boost::detail::thread_move_t' to 'const MyClass &'
1> with
1> [
1> T=MyClass
1> ]
1> Reason: cannot convert from 'boost::detail::thread_move_t' to 'const MyClass'
1> with
1> [
1> T=MyClass
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(292) : while compiling class template member function 'boost::optional_detail::optional_base::optional_base(boost::optional_detail::optional_base &&)'
1> with
1> [
1> T=MyClass
1> ]
1> c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(873) : see reference to function template instantiation 'boost::optional_detail::optional_base::optional_base(boost::optional_detail::optional_base &&)' being compiled
1> with
1> [
1> T=MyClass
1> ]
1> c:\workspace\third_party\boost_1_59_0\boost/optional/optional.hpp(766) : see reference to class template instantiation 'boost::optional_detail::optional_base' being compiled
1> with
1> [
1> T=MyClass
1> ]
1> main.cpp(14) : see reference to class template instantiation 'boost::optional' being compiled
Note the #include "boost/thread.hpp". When removing this include the code compiles. Anything that can be done to workaround?

You must define BOOST_THREAD_USES_MOVE before you use any boost header.
#define BOOST_THREAD_USES_MOVE
More information are located here. This define emulates a move by Boost.Move which is necessary here.
In order to implement Movable classes, move parameters and return
types Boost.Thread uses the rvalue reference when the compiler support
it. On compilers not supporting it Boost.Thread uses either the
emulation provided by Boost.Move or the emulation provided by the
previous versions of Boost.Thread depending whether
BOOST_THREAD_USES_MOVE is defined or not. This macros is unset by
default when BOOST_THREAD_VERSION is 2. Since BOOST_THREAD_VERSION 3,
BOOST_THREAD_USES_MOVE is defined.
Also see Boost.Move:
Boost.Thread uses by default an internal move semantic implementation.
Since version 3.0.0 you can use the move emulation emulation provided
by Boost.Move.
When BOOST_THREAD_VERSION==2 define BOOST_THREAD_USES_MOVE if you want
to use Boost.Move interface. When BOOST_THREAD_VERSION==3 define
BOOST_THREAD_DONT_USE_MOVE if you don't want to use Boost.Move
interface.

Related

Cannot pass std::vector<bool> to winrt::array_view

I trying to consume Windows::Gaming::Input::RawGameController via C++/WinRT library.
Calling RawGameController::GetCurrentReading() to acquire current controller state:
std::vector<bool> buttonsArray(rawController.ButtonCount(), false);
std::vector<GameControllerSwitchPosition> switchesArray(rawController.SwitchCount(), GameControllerSwitchPosition::Center);
std::vector<double> axisArray(rawController.AxisCount(), 0.0);
uint64_t timestamp = rawController.GetCurrentReading(buttonsArray, switchesArray, axisArray);
And have compile error:
1>------ Build started: Project: cppwinrtgamepad, Configuration: Debug x64 ------
1>cppwinrtgamepad.cpp
1>c:\somepath\x64\debug\generated files\winrt\base.h(3458): error C2039: 'data': is not a member of 'std::vector<T,std::allocator<_Ty>>'
1> with
1> [
1> T=bool,
1> _Ty=bool
1> ]
1>c:\somepath\x64\debug\generated files\winrt\base.h(3663): note: see declaration of 'std::vector<T,std::allocator<_Ty>>'
1> with
1> [
1> T=bool,
1> _Ty=bool
1> ]
1>c:\somepath\cppwinrtgamepad.cpp(121): note: see reference to function template instantiation 'winrt::array_view<T>::array_view<T>(std::vector<T,std::allocator<_Ty>> &) noexcept' being compiled
1> with
1> [
1> T=bool,
1> _Ty=bool
1> ]
1>c:\somepath\cppwinrtgamepad.cpp(121): note: see reference to function template instantiation 'winrt::array_view<T>::array_view<T>(std::vector<T,std::allocator<_Ty>> &) noexcept' being compiled
1> with
1> [
1> T=bool,
1> _Ty=bool
1> ]
1>c:\somepath\cppwinrtgamepad.cpp(90): note: see reference to class template instantiation 'winrt::impl::fast_iterator<winrt::Windows::Foundation::Collections::IVectorView<winrt::Windows::Gaming::Input::Gamepad>>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(7801): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IContextCallback>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(7573): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IServerSecurity>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(7532): note: see reference to class template instantiation 'std::chrono::time_point<winrt::clock,winrt::Windows::Foundation::TimeSpan>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(5264): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IMarshal>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(2503): note: see reference to class template instantiation 'winrt::com_ptr<To>' being compiled
1> with
1> [
1> To=winrt::impl::ILanguageExceptionErrorInfo2
1> ]
1>c:\somepath\x64\debug\generated files\winrt\base.h(4120): note: see reference to function template instantiation 'winrt::com_ptr<To> winrt::com_ptr<winrt::impl::IRestrictedErrorInfo>::try_as<winrt::impl::ILanguageExceptionErrorInfo2>(void) noexcept const' being compiled
1> with
1> [
1> To=winrt::impl::ILanguageExceptionErrorInfo2
1> ]
1>c:\somepath\x64\debug\generated files\winrt\base.h(4202): note: see reference to class template instantiation 'winrt::com_ptr<winrt::impl::IRestrictedErrorInfo>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\string_view(39): note: see reference to class template instantiation 'std::basic_string_view<wchar_t,std::char_traits<wchar_t>>' being compiled
1>c:\somepath\x64\debug\generated files\winrt\base.h(3458): error C2664: 'winrt::array_view<T>::array_view(winrt::array_view<T> &&)': cannot convert argument 1 from 'winrt::array_view<T>::size_type' to 'std::initializer_list<bool>'
1> with
1> [
1> T=bool
1> ]
1>c:\somepath\x64\debug\generated files\winrt\base.h(3459): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>Done building project "cppwinrtgamepad.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
GetCurrentReading() is defined in winrt/Windows.Gaming.Input.h like this:
template <typename D> uint64_t consume_Windows_Gaming_Input_IRawGameController<D>::GetCurrentReading(array_view<bool> buttonArray, array_view<Windows::Gaming::Input::GameControllerSwitchPosition> switchArray, array_view<double> axisArray) const
And corresponding winrt::array_view constructor is defined in winrt/base.h like this:
template <typename C>
array_view(std::vector<C>& value) noexcept :
array_view(value.data(), static_cast<size_type>(value.size()))
{}
Looks like oblivious bug considering that std::vector<bool> doesn't contrain data() method at all.
Or there is other recommended way to call RawGameController::GetCurrentReading()?
PS: as a workaround I could use std::array<bool, SOME_BIG_BUTTTON_COUNT> but its so ugly.
This is by design. The winrt::array_view is an adapter that tells the underlying API that the bound array or storage has the appropriate binary layout to receive the data efficiently (typically via a memcpy) without some kind of transformation. std::vector<bool> does not provide that guarantee and thus cannot be used. You might want to try something else like a std::array or some other contiguous container.
Ugly workaround instead of using vector<bool>:
int32_t buttons = rawController.ButtonCount();
int32_t switches = rawController.SwitchCount();
int32_t axis = rawController.AxisCount();
std::unique_ptr<bool[]> buttonsArray = std::make_unique<bool[]>(buttons);
std::vector<GameControllerSwitchPosition> switchesArray(switches);
std::vector<double> axisArray(axis);
uint64_t timestamp = rawController.GetCurrentReading(winrt::array_view<bool>(buttonsArray.get(), buttonsArray.get() + buttons), switchesArray, axisArray);
Each element in std::vector<bool> occupies a single bit instead of sizeof(bool) bytes.It does not necessarily store its elements as a contiguous array and thus doesn't contain data() method.I have a working code, but it is not the optimal solution.You can try to create bool array by using bool b[] method.
bool buttonsArray[]{ rawController.ButtonCount(), false };
std::vector<GameControllerSwitchPosition> switchesArray(rawController.SwitchCount(), GameControllerSwitchPosition::Center);
std::vector<double> axisArray(rawController.AxisCount(), 0.0);
uint64_t timestamp = rawController.GetCurrentReading(buttonsArray, switchesArray, axisArray);

std::unique_ptr works in GCC but won't compile in Visual Studio [duplicate]

This question already has answers here:
Why is this code trying to call the copy constructor?
(2 answers)
Closed 4 years ago.
It's taken me a while, but I've finally constructed a minimal example with illustrates the problem I'm having.
#include <memory>
#include <vector>
class Thing
{
};
class Box
{
public:
std::vector<std::unique_ptr<Thing>> Things;
static Box MakeBox() {Box b; return b;}
};
My real program is obviously quite a lot more complicated than this.
GCC 4.8.3 happily compiles this. It also compiles the real application, which works perfectly.
Visual Studio 2012 insists that this code is not correct, giving me error C2248 on line 606 of vc\include\xmemory0. If I wade through several miles of compiler output, I discover the real source of the error is line 11 in the above example. (The line that defines Things.) VS also refuses to compile my real application, with the same error.
So, is this code correct or not? If it's not correct, then why does GCC accept it? And how to I make it correct? If it is correct, then why won't VS compile it? Is there some way I can unconditionally force VS to actually compile my program?
Output from VS:
1>------ Build started: Project: TestUniquePtr, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=Thing
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<std::unique_ptr<Thing>>
1> ]
1> d:\projects\c++\testuniqueptr\testuniqueptr\main.cpp(11) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Thing>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Your problem isn't std::unique_ptr but std::vector.
Your compiler comes with an old version of std::vector that requires the element type to be copyable.
The return by value (return b;) should invoke a move of the vector, but your std::vector doesn't implement move.
std::unique_ptr is moveable but not copyable, therefore it doesn't meet the pre-C++11 requirements for being used in std::vector... a requirement which still applies to VC++ 2012.
Your best option is to use a newer compiler and standard library. One that supports move semantics on std::vector.
Otherwise you might make some progress by eliminating the copy of std::vector, for example, by having MakeBox fill in an output argument rather than returning a new object.
static void MakeBox(Box& b) { /* fill Things in b provided by caller */ }
That's probably an exercise in futility though, because whenever the vector needs to grow, it has to relocate the existing elements to new storage, and with incomplete move support, it will try to copy those.
The problem is that Box has no move constructor, thus returning a Box requires it to have a copy constructor (which it can't because unique_ptr is not copyable). All you have to do is define a move constructor for Box:
Box::Box(Box&& other)
: Things(std::move(other.Things))
{
}
With more recent editions, the compiler will generate the move constructor for you.

Trouble move-capturing std::unique_ptr in a lambda using std::bind

I'd like to capture a variable of type std::vector<std::unique_ptr<MyClass>> in a lambda expression (in other words, "capture by move"). I found a solution which uses std::bind to capture unique_ptr (https://stackoverflow.com/a/12744730/2478832) and decided to use it as a starting point. However, the most simplified version of the proposed code I could get doesn't compile (lots of template mistakes, it seems to try to call unique_ptr's copy constructor).
#include <functional>
#include <memory>
std::function<void ()> a(std::unique_ptr<int>&& param)
{
return std::bind( [] (int* p) {},
std::move(param));
}
int main()
{
a(std::unique_ptr<int>(new int()));
}
Can anybody point out what is wrong with this code?
EDIT: tried changing the lambda to take a reference to unique_ptr, it still doesn't compile.
#include <functional>
#include <memory>
std::function<void ()> a(std::unique_ptr<int>&& param)
{
return std::bind( [] (std::unique_ptr<int>& p) {}, // also as a const reference
std::move(param));
}
int main()
{
a(std::unique_ptr<int>(new int()));
}
Here's Visual Studio 2012 output:
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\tuple(151): error C2248: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<int,std::default_delete<_Ty>>'
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1447) : see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr'
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\tuple(521) : see reference to function template instantiation 'std::_Tuple_val<_This>::_Tuple_val<const _Ty&>(_Other)' being compiled
1> with
1> [
1> _This=std::unique_ptr<int,std::default_delete<int>>
1> , _Ty=std::unique_ptr<int,std::default_delete<int>>
1> , _Other=const std::unique_ptr<int,std::default_delete<int>> &
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\tuple(521) : see reference to function template instantiation 'std::_Tuple_val<_This>::_Tuple_val<const _Ty&>(_Other)' being compiled
1> with
1> [
1> _This=std::unique_ptr<int,std::default_delete<int>>
1> , _Ty=std::unique_ptr<int,std::default_delete<int>>
1> , _Other=const std::unique_ptr<int,std::default_delete<int>> &
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\tuple(521) : while compiling class template member function 'std::tuple<std::unique_ptr<int,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::tuple(const std::tuple<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> &)'
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152) : see reference to function template instantiation 'std::tuple<std::unique_ptr<int,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::tuple(const std::tuple<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> &)' being compiled
1> with
1> [
1> _Ty=int
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152) : see reference to class template instantiation 'std::tuple<std::unique_ptr<int,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' being compiled
1> with
1> [
1> _Ty=int
1> ]
1> main.cpp(15) : see reference to class template instantiation 'std::_Bind<false,void,a::<lambda_2ad08ede4c4ce9c02d5497417b633d1d>,std::unique_ptr<int,std::default_delete<_Ty>>,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' being compiled
1> with
1> [
1> _Ty=int
1> ]
The second argument to bind will be passed to the bound object at the time of call. The problem is that the lambda takes a int*, but the argument is a std::unique_ptr<int> and there is no conversion from the latter to the former.
It should compile (untested) if you change the signature of the lambda to take a std::unique_ptr by reference/const-reference
The problem with the version that passes a lambda taking unique_ptr by reference to std::bind is your conversion to std::function - std::function requires functions to be CopyConstructible ([func.wrap.func.con] p7). Try it without the std::function (Live at ideone):
auto f = std::bind([](std::unique_ptr<int>&){},
std::make_unique<int>());
My understanding of the internals of std::bind is that it will always make a copy of the 1st argument object that is being bound to the function-object rather than moving it (even if that argument is an rvalue), so you'll always end up with a call to the copy-constructor for whatever object you are attempting to bind to the function object, and not the move-constructor, even with the use of std::move.

Boost fast pool allocator has problems with function pointers?

Is this a Boost bug or am I doing something wrong?
#include <map>
#include <boost/pool/pool_alloc.hpp>
int main()
{
typedef const std::string key;
typedef double* (*value)(const int&);
std::map<key, value, std::less<key>> map_with_standard_allocator; // works
std::map<key, value, std::less<key>, boost::fast_pool_allocator<std::pair<const key, value> > > map_with_boost_allocator; // fails
}
the last line fails to compile under MS Visual Studio 2008 with Boost 1.40 and 1.48. It compiles fine under g++ 4.5.3 (Cygwin), though.
The error is:
1>Compiling...
1>main.cpp
1>C:\UniLib1\trunk\External\boost/pool/pool_alloc.hpp(205) : error C2535: 'const std::basic_string<_Elem,_Traits,_Ax> *boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>::address(const std::basic_string<_Elem,_Traits,_Ax> &)' : member function already defined or declared
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> C:\UniLib1\trunk\External\boost/pool/pool_alloc.hpp(202) : see declaration of 'boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>::address'
1> with
1> [
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> c:\Program Files\Microsoft Visual Studio 9.0\VC\include\xtree(137) : see reference to class template instantiation 'boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>' being compiled
1> with
1> [
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> c:\Program Files\Microsoft Visual Studio 9.0\VC\include\map(78) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<key,value ,std::less<key>,boost::fast_pool_allocator<std::pair<key,value >>,false>
1> ]
1> .\main.cpp(9) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1> with
1> [
1> _Kty=key,
1> _Ty=value,
1> _Pr=std::less<key>,
1> _Alloc=boost::fast_pool_allocator<std::pair<key,value >>
1> ]
This is not a bug in VS2008 (as I mistakenly claimed in an earlier edit to this answer). The C++03 standard requires that the key type for an associative container, like std::map, must be 'assignable' (per Table 69 in 23.1.2 "Associative containers"). And a const std::string is not assignable. Note that the C++11 standard seems to relax this requirement, but the new standard doesn't apply to since VC++ 2008.
It's not clear to me that a compiler is required to diagnose code that tries to use std::map with a non-assignable key, so I don't think one can claim that GCC or VC++ 2010 are accepting this code improperly (I think it falls into the area of undefined code that works as you might expect, even though there's no guarantee it'll work). However it is clear that it's OK for VC++ 2008 to refuse to compile it.
All that said, I think that VC++ 2008's library parameterizing the allocator's address() function on the map's key rather than map's element is still suspect (see the first edit of this answer for details if you're interested), but I don't think there's any real bug there since the std::pair<> used to hold the map element will always be set up such that key part will be at the same address as the whole element.

I do not understand this C++ error - error C2101: '&' on constant

This code supposedly works with GCC - I am trying to get it to work with Visual Studio. I can't figure out if the code is actually faulty or I'm not doing something right with the port.
1>c:\somepath\aaa.h(52): error C2101: '&' on constant
1> c:\somepath\aaa.h(52): while compiling class template member function 'const blahblah::Message something::AClass<Type>::aMethod(void) const'
1> with
1> [
1> Type=const lala::BClass&
1> ]
1> c:\somepath\bbb.h(79) : see reference to class template instantiation 'something:AClass<Type>' being compiled
1> with
1> [
1> Type=const lala::BClass&
1> ]
1> MyApplication.cpp
Files
aaa.h:52 virtual const Type aMethod() const { return Type(); }
bbb.h:79 AClass<const BClass&> blahblahblah_;
Constructing T() where T is a reference type is not valid, and makes no sense. Some versions of gcc incorrectly accept it.