如何将指向字符串映射的智能指针存储到 class 中的成员函数,以便它不会因类型不完整而失败?
How to store a smart pointer to map of string to member function in a class so that it doesn't fail due to an incomplete type?
背景
我一直在写一个 StateMachine,它的 t运行sition table 在 运行 时间加载。对每个 t运行sition 采取的操作存储为字符串。该字符串被转换为指向状态机 class 成员函数的 std::function
对象。当事件发生并导致 t运行sition 时,将调用该函数。
问题
我之前成功使用过这个策略来决定在运行时调用哪个函数。不幸的是,我 运行 遇到以下错误:
error: return type 'XStMachine::TrFunc {aka class std::function<void (XStMachine::*)(const EventData&)>}' is incomplete
或
invalid use of incomplete type
已走步数
我咨询了Google和Whosebug。我有很多想法,包括将定义从类型不完整的地方移除。不幸的是,我无法让它成功运行。
我尝试使用原始指针而不是 unique_ptr
并发现事情神奇地起作用了。
我最后阅读了一些关于 shared_ptr
和 unique_ptr
如何处理不完整类型的区别。我尝试了 shared_ptr,但这也没有解决我的问题。
我尝试为我的状态机创建一个 friend
class,希望在朋友 class 声明时,类型将是视为整体。我无法让它工作。
最后,我创建了以下最小示例(请取消注释代码以重现错误。)它演示了我 运行 遇到的问题: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;
^~~~~~~~
目标
初级: 了解示例代码中发生的事情并修复它。
中学:清楚地了解什么是不完整类型,这样我就可以:
* 解决未来的相关问题。
* 知道用调用默认值的删除器覆盖 unique_ptr 的默认删除器是否安全。
我的不理解真的妨碍了我。
相关问题
即使在示例代码中 Friendly
被声明为 XStMachine
中的友元,它也必须在程序的前面进行前向声明。为什么会这样?
即使 Friendly
被声明为友元,它也不能访问 XStMachine
的受保护成员函数。例如,&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
并直接在映射中存储指向成员函数的指针。
背景
我一直在写一个 StateMachine,它的 t运行sition table 在 运行 时间加载。对每个 t运行sition 采取的操作存储为字符串。该字符串被转换为指向状态机 class 成员函数的 std::function
对象。当事件发生并导致 t运行sition 时,将调用该函数。
问题
我之前成功使用过这个策略来决定在运行时调用哪个函数。不幸的是,我 运行 遇到以下错误:
error: return type 'XStMachine::TrFunc {aka class std::function<void (XStMachine::*)(const EventData&)>}' is incomplete
或
invalid use of incomplete type
已走步数
我咨询了Google和Whosebug。我有很多想法,包括将定义从类型不完整的地方移除。不幸的是,我无法让它成功运行。
我尝试使用原始指针而不是
unique_ptr
并发现事情神奇地起作用了。我最后阅读了一些关于
shared_ptr
和unique_ptr
如何处理不完整类型的区别。我尝试了 shared_ptr,但这也没有解决我的问题。我尝试为我的状态机创建一个
friend
class,希望在朋友 class 声明时,类型将是视为整体。我无法让它工作。最后,我创建了以下最小示例(请取消注释代码以重现错误。)它演示了我 运行 遇到的问题: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;
^~~~~~~~
目标
初级: 了解示例代码中发生的事情并修复它。
中学:清楚地了解什么是不完整类型,这样我就可以: * 解决未来的相关问题。 * 知道用调用默认值的删除器覆盖 unique_ptr 的默认删除器是否安全。
我的不理解真的妨碍了我。
相关问题
即使在示例代码中
Friendly
被声明为XStMachine
中的友元,它也必须在程序的前面进行前向声明。为什么会这样?即使
Friendly
被声明为友元,它也不能访问XStMachine
的受保护成员函数。例如,&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
并直接在映射中存储指向成员函数的指针。