gpt4 book ai didi

c++ - 如何存储智能指针以将字符串映射到类中的成员函数,以便它不会因类型不完整而失败?

转载 作者:行者123 更新时间:2023-11-30 02:26:34 27 4
gpt4 key购买 nike


背景


我一直在编写一个状态机,其转换表在运行时加载。对每个转换采取的操作存储为字符串。字符串被转换为 std::function指向状态机类成员函数的对象。当事件发生并导致转换时,将调用该函数。


问题


我之前已经成功地使用这个策略来决定在运行时调用哪个函数。不幸的是,我遇到了以下错误:

error: return type 'XStMachine::TrFunc {aka class std::function<void (XStMachine::*)(const EventData&)>}' is incomplete

或者

invalid use of incomplete type


采取的步骤


  1. 我咨询了 Google 和 Stackoverflow。我有很多想法,包括将定义从类型不完整的地方移除。不幸的是,我无法让它成功运行。

  2. 我尝试使用原始指针而不是 unique_ptr并发现事情神奇地起作用了。

  3. 我最后阅读了一些关于 shared_ptr 之间的区别的内容。和 unique_ptr处理不完整的类型。我尝试了 shared_ptr,但这也没有解决我的问题。

  4. 我尝试创建一个 friend类到我的状态机,希望在 friend 类声明时,该类型被认为是完整的。我无法让它工作。

  5. 最后,我创建了以下最小示例(请取消注释代码以重现错误。)它演示了我遇到的问题:http://coliru.stacked-crooked.com/a/791092c7ca8fff24并请来了专家! :)


源代码


#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include <map>
#include <iomanip>

using namespace std;

struct EventData
{
unsigned int x;
};

class Friendly; // Required for compiling the code. Why?

class XStMachine
{
friend class Friendly;
unique_ptr<Friendly> fPtr; //-- Doesn't compile if Friendly is not fwd. declared
unsigned int y;
unsigned int z;
public:
typedef void(XStMachine::*TrFuncPtr)(EventData const&);
typedef std::function<TrFuncPtr> TrFunc;
private:
// map<string, TrFunc> fMap; //-- Doesn't compile because TrFunc is incomplete
// unique_ptr<map<string, TrFunc>> fMap; // Doesn't compile; incomplete type.
map<string, TrFunc> *fMap; // Compiles with incomplete type.
protected:
void tranFunc1(EventData const &d)
{
y = d.x;
}

void tranFunc2(EventData const &d)
{
z = d.x;
}
public:
XStMachine()
{
// Code to init fMap
}

// The code below doesn't compile. incomplete type.
TrFunc getTranFunc(std::string const &s)
{
return (*fMap)[s];
}

~XStMachine()
{
}
};

class Friendly
{
// unique_ptr<map<string, XStMachine::TrFunc> fMap; // Doesn't compile, the type is incomplete.
public:
Friendly()
{
// Code to allocate and init fMap
}

// Dosen't compile if defined here because the return type is incomplete.
//XStMachine::TrFunc& getTranFunc(std::string const&)
//{
// return (*fMap)[s];
//}
};
// The type is incomplete -> Will this work inside a separate cpp file?
//XStMachine::TrFunc& getTranFunc(std::string const &s)
//{
// Weird - Can't access protected members though we're friends. :/
/*
static map<string, XStMachine::TrFunc> fMap = {{"tranFunc1", function(&XStMachine::tranFunc1)},
{"tranFunc2", function(&XStMachine::tranFunc2)}
};
*/
//return fMap[s];
//}

int main() {
cout << "I need to understand incomplete types." << endl;
return 0;
}

Coliru(gcc 6.3、C++ 14)的完整错误输出


main.cpp: In member function 'XStMachine::TrFunc XStMachine::getTranFunc(const string&)':
main.cpp:48:3: error: return type 'XStMachine::TrFunc {aka class std::function<void (XStMachine::*)(const EventData&)>}' is incomplete
{
^
In file included from /usr/local/include/c++/6.3.0/bits/stl_algobase.h:64:0,
from /usr/local/include/c++/6.3.0/bits/char_traits.h:39,
from /usr/local/include/c++/6.3.0/ios:40,
from /usr/local/include/c++/6.3.0/ostream:38,
from /usr/local/include/c++/6.3.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/6.3.0/bits/stl_pair.h: In instantiation of 'struct std::pair<const std::__cxx11::basic_string<char>, std::function<void (XStMachine::*)(const EventData&)> >':
/usr/local/include/c++/6.3.0/bits/stl_map.h:481:10: required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::function<void (XStMachine::*)(const EventData&)>; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::function<void (XStMachine::*)(const EventData&)> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::function<void (XStMachine::*)(const EventData&)>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::__cxx11::basic_string<char>]'
main.cpp:49:23: required from here
/usr/local/include/c++/6.3.0/bits/stl_pair.h:196:11: error: 'std::pair<_T1, _T2>::second' has incomplete type
_T2 second; /// @c second is a copy of the second object
^~~~~~
In file included from main.cpp:3:0:
/usr/local/include/c++/6.3.0/functional:1526:11: note: declaration of 'class std::function<void (XStMachine::*)(const EventData&)>'
class function;
^~~~~~~~

目标


初级:了解示例代码中发生的事情并修复它。

中等:清楚地了解什么是不完整类型,这样我就可以: * 解决 future 的相关问题。 * 知道用调用默认值的删除器覆盖 unique_ptr 的默认删除器是否安全。

我缺乏理解真的妨碍了我。


相关问题


  1. 即使FriendlyXStMachine 内声明为 friend 在示例代码中,它也必须在程序的前面声明。为什么会这样?

  2. 即使Friendly被声明为友元,它不能访问 XStMachine 的 protected 成员函数.例如,&XStMachine::tranFunc1是无效的。为什么?

最佳答案

std::function仅采用常规函数类型作为模板参数。指向成员函数的指针类型不起作用。

以下是 std::function 的典型定义在标准库中:

template< class >
class function; // intentionally undefined
template< class R, class... Args >
class function<R(Args...)> // actual definition

模板参数并不决定这个实例化可以存储什么样的函数,而是如何调用这个实例化

任何使用非常规函数类型的实例化尝试都将产生不完整的类型。示例:

std::function <int> incomplete;

在您的代码中,您可以:

  • 店铺 std::function<void(EventData const&)>在映射中(使用 std::bind 从指向成员函数的指针和对象指针构造这些对象);或
  • 取消std::function完全一致并直接在映射中存储指向成员函数的指针。

关于c++ - 如何存储智能指针以将字符串映射到类中的成员函数,以便它不会因类型不完整而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42667952/

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