gpt4 book ai didi

c++ - main()::__lambda1 不是 std::_Tuple_impl<1ul, main()::__lambda1> 的可访问基类

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:11 26 4
gpt4 key购买 nike

采取以下措施:

#include <utility>
#include <functional>
#include <iostream>

struct FooType
{
FooType(std::tuple<std::function<void()>, std::function<void()>> t)
{
std::get<0>(t)();
std::get<1>(t)();
}
};

int main()
{
FooType foo(
std::make_tuple(
[]() { std::cout << "a\n"; },
[]() { std::cout << "b\n"; }
)
);
}

它构建并运行良好 with GCC 4.9和更高,但是 with GCC 4.8 (这是我的构建系统)以下编译器错误结果:

In file included from main.cpp:2:0:
/usr/include/c++/4.8/functional: In substitution of 'template<class _Res, class ... _ArgTypes> template<class _Functor> using _Invoke = decltype (std::__callable_functor(declval<_Functor&>())((declval<_ArgTypes>)()...)) [with _Functor = std::_Tuple_impl<1ul, main()::__lambda1>; _Res = void; _ArgTypes = {}]':
/usr/include/c++/4.8/functional:2257:9: required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _UHead, _UTails ...>&&) [with _UHead = main()::__lambda0; _UTails = {main()::__lambda1}; long unsigned int _Idx = 0ul; _Head = std::function<void()>; _Tail = {std::function<void()>}]'
/usr/include/c++/4.8/tuple:556:60: required from 'constexpr std::tuple<_T1, _T2>::tuple(std::tuple<_U1, _U2>&&) [with _U1 = main()::__lambda0; _U2 = main()::__lambda1; <template-parameter-2-3> = void; _T1 = std::function<void()>; _T2 = std::function<void()>]'
main.cpp:21:5: required from here
/usr/include/c++/4.8/functional:2181:71: error: 'main()::__lambda1' is not an accessible base of 'std::_Tuple_impl<1ul, main()::__lambda1>'
using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
^

大概我在 GCC 4.8 的实验性 C++11 实现中遇到了一个“错误”,但我在网上找不到任何关于它的讨论。这个问题对任何人都很熟悉吗?有便宜的解决方法吗?

在元组中传递 std::function 是我刚刚创建的设计的核心——我很遗憾在 Coliru 中构建它而没有特别切换到 GCC 4.8 直到最后!

最佳答案

使用显式模板参数创建元组 works as a workaround :

#include <utility>
#include <functional>
#include <iostream>

struct FooType
{
FooType(std::tuple<std::function<void()>, std::function<void()>> t)
{
std::get<0>(t)();
std::get<1>(t)();
}
};

int main()
{
FooType foo(
std::make_tuple
<
std::function<void()>,
std::function<void()>
>(
[]() { std::cout << "a\n"; },
[]() { std::cout << "b\n"; }
)
);
}

关于c++ - main()::__lambda1 不是 std::_Tuple_impl<1ul, main()::__lambda1> 的可访问基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46975042/

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