gpt4 book ai didi

c++ - 无法将 std::vector 传递给 winrt::array_view

转载 作者:搜寻专家 更新时间:2023-10-31 01:25:18 25 4
gpt4 key购买 nike

我正在尝试消费 Windows::Gaming::Input::RawGameController通过C++/WinRT图书馆。

调用 RawGameController::GetCurrentReading()获取当前 Controller 状态:

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>但它太丑了。

最佳答案

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

关于c++ - 无法将 std::vector<bool> 传递给 winrt::array_view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com