无法将std :: vector <bool>传递给winrt :: array_view

时间:2019-07-16 14:27:12

标签: c++ uwp c++17 c++-winrt

我试图通过Windows::Gaming::Input::RawGameController库使用C++/WinRT

调用RawGameController::GetCurrentReading()获取当前控制器状态:

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);

并有编译错误:

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()winrt/Windows.Gaming.Input.h中的定义如下:

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

winrt::array_view中定义了相应的winrt/base.h构造函数,如下所示:

template <typename C>
array_view(std::vector<C>& value) noexcept :
    array_view(value.data(), static_cast<size_type>(value.size()))
{}

考虑到std::vector<bool>完全不违反data()方法,这看起来像是一个遗忘的错误。 还是有其他推荐的致电RawGameController::GetCurrentReading()的方式?

PS:作为一种解决方法,我可以使用std::array<bool, SOME_BIG_BUTTTON_COUNT>,但它是如此丑陋。

3 个答案:

答案 0 :(得分:3)

这是设计使然。 winrt::array_view是一个适配器,它告诉基础API绑定的数组或存储具有适当的二进制布局,可以有效地(通常通过memcpy来接收数据,而无需进行某种转换。 std::vector<bool>不提供该保证,因此无法使用。您可能想尝试其他类似std::array或其他连续容器的方法。

答案 1 :(得分:0)

std::vector<bool>中的每个元素占用一个位而不是sizeof(bool)字节。它不一定将其元素存储为连续数组,因此不包含data()方法。工作代码,但这不是最佳解决方案。您可以尝试使用bool b []方法创建bool数组。

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);

答案 2 :(得分:0)

丑陋的解决方法,而不是使用$query=" insert gis_news_items (title, alias, introtext, fulltexts, catid, published, modified_by, metadesc, metakey ) values ('".$title."', '".$this->input->post('alias',true )."', '". $this->input->post('content')."',' ', ".$this->input->post('catid').", '".$this->input->post('pub').$usertxt."', ".$this->user.$tgl.", '".str_replace('"',"'", $this->input->post('deskripsi',true) )."', '".str_replace('"',"'", $this->input->post('keyword',true) )."' ) ";

vector<bool>