背景
我一直在编写一个StateMachine,它的转换表在运行时加载。要对每个转换采取的操作存储为一个字符串。字符串被转换为指向状态机类的成员函数的std::function对象。当发生事件并导致转换时,将调用该函数。
问题
我以前已经成功地使用了这个策略来决定在运行时调用哪个函数。不幸的是,我遇到了以下错误:
error: return type 'XStMachine::TrFunc {aka class std::function<void (XStMachine::*)(const EventData&)>}' is incomplete
这个问题可能有点粗略,因为我家里没有可用的代码,但我知道这件事会困扰我整个周末。
当我试图将一些代码更新为C++11时,我开始用std::unordered_map替换一些std::map。代码只使用std::map::find()访问映射中的特定元素,因此我认为替换应该很容易。返回的迭代器存储在一个auto-typed变量(auto res = map.find( x ) )中,所以输入应该很好。然而,当使用res->second.do_stuff()访问存储的元素时,我得到了一个编译器错误,告诉我,struct std::pair<char, B> does not ha
template <typename Type, Type Func>
struct A
{
};
void func();
A<void(), func> a; // same result with A<void(), &func> a;
此代码使用Clang编译(包括最新的8.0.0),而不是用GCC编译(包括最新的9.1)。
GCC说:error: 'void()' is not a valid type for a template non-type parameter
哪个编译器是正确的,为什么?
更新
我猜GCC是错
关于,Ben提供的社区wiki答案讨论了内联实例化函数模板的问题。
在这个答案中有以下代码:
template<typename OP>
int do_op(int a, int b, OP op)
{
return op(a,b,);
}
int add(int a, b) { return a + b; }
int (* func_ptr)(int, int) = add;
int c = do_op(4,5,func_ptr);
答案接着这样说(关于最后一行,它实例化函数模板do_op):
很明显这不是内线。
我的问题是:为什么很明显,这不是内线呢?
所以我今天遇到了关于C++模板编程的问题,谁能给我解释一下A(*)(B)作为模板参数是什么?
template <class X, class Y, class A, class B>
struct replace_type_impl<A(*)(B),X,Y,false>
{
typedef typename replace_type<A,X,Y>::type (*type)(typename replace_type<B,X,Y>::type);
};
因此,我有一个函数,它接受一个映射作为参数,如下所示。
function do_dope_stuff(int arg1, std::map<uint32_t, parent_class>& my_object){
//do some programmer magic
}
然后从几个不同的地方调用这个函数,其中一个映射ID的对象类型是脱皮的。
do_dope_stuff(int, std::map<uint32_t, child_class1>&)
or
do_dope_stuff(int, std::map<uint32_t, chi
我有一个关于std::vectors中对象指针的有效性的问题。我不确定以下示例中的内存是否被正确保存,以及是否以我想要的方式访问这些指针:
// class which should hold the 4 Indices
struct Face
{
Face(){};
Face(std::array<int, 4> indices) : Indices(indices) // array is copied and saved in face object
{}
std::array<int, 4> Indices{};
}
stati
嘿,我想在c++中实现委托,我知道如何在c#中实现,我发布了我的代码,但不知道如何将其转换为c++
public class Events {
public delegate void Action();
public Action OnPrintTheText = delegate{};
}
public class ABC {
private Event evt;
public ABC() {
evt = new Event();
}
public void printText() {
evt.OnPrin
using FuncDef = void(int a, int b);
template <typename R, typename... Args>
class WrongFunction {
public:
void F1(R(*Fn)(Args...)) {}//error: C2091 function returns function
};
template <typename T> class Function;
template <typename R, typename... Args>
class Function<R(A
我尝试使用以下命令声明PCL类的对象
typedef pcl::PointCloud<PointNT> PointCloudT; // in the .h file.
和
PointCloudT::Ptr object = new PointCloudT(); // in main func
PointCloudT::Ptr scene = new PointCloudT(); // and these two triggers the error of
初始化错误C2440:‘1>f:\cpps\pclrclass\pclrclass\main_routine
关于会员指针我有个问题。以下代码无法使用12.2的CC和cygwin 4.3.4进行编译,但使用的是Microsoft C++ 2010:
struct A {
int x;
};
struct B : public A {
};
template<typename T> class Bar {
public:
template<typename M> void foo(M T::*p);
};
int main(int, char *[]) {
Bar<B> bbar;
bbar.foo(&B::x);
ret
我想知道在C++中是否有一种方法可以编写一个仅基于方法名来解析对象方法的函数(下面的具体例子)。运行时或编译时解析(例如模板)都是可以接受的。我读过一些关于模板元编程的文章,但我希望在深入研究这个问题之前,我可以得到一些关于这是否是我问题的合适方向的信息。
本质上,我试图编写一个函数来调用传入对象的非静态方法,例如这个psuedocode:
exampleFunction(Object myObject, ObjectMethod myObjectMethod) {
// do some stuff here
myObject.myObjectMethod(arguments