规则是什么?OTOH的简单案例似乎意味着新类型是最后一件事.像这里Uchar是新类型:
typedef unsigned char Uchar;
Run Code Online (Sandbox Code Playgroud)
但是函数指针完全不同.这里的新类型是pFunc:
typedef int (*pFunc)(int);
Run Code Online (Sandbox Code Playgroud)
我无法想到任何其他的例子,但我遇到了一些非常令人困惑的用法.
那么有规则还是人们应该从经验中知道这是怎么做的,因为他们之前已经看过这样做了?
另外:a的范围是typedef什么?
#include <functional>
struct A
{
int func(int x, int y)
{
return x+y;
}
};
int main()
{
typedef std::function<int(int, int) > Funcp;
A a;
//Funcp func = std:::bind(&A::func, &a);
Funcp func = std::bind(&A::func, a, std::placeholders::_1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在上述两个绑定函数中都遇到错误:
error C2825: '_Fty': must be a class or namespace when followed by '::'
Run Code Online (Sandbox Code Playgroud)
语法错误在哪里?我正在使用visual studio 2010
我怎样才能比较两个C++ 11个std::functions的operator==,并返回true如果两个所述functions请参阅同一个函数指针?
由于该函数是存储在一个连续内存块中的指令集.
并且函数(入口点)的地址是函数中第一条指令的地址.(据我所知)
因此我们可以说函数的地址和函数中第一条指令的地址是相同的(在这种情况下,第一条指令是变量的初始化.).
但是下面的程序与上述内容相矛盾.
码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ** fun()
{
static char * z = (char*)"Merry Christmas :)";
return &z;
}
int main()
{
char ** ptr = NULL;
char ** (*fun_ptr)(); //declaration of pointer to the function
fun_ptr = &fun;
ptr = fun();
printf("\n %s \n Address of function = [%p]", *ptr, fun_ptr);
printf("\n Address of first variable created in fun() = [%p]", (void*)ptr);
cout<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一个输出示例是:
Merry Christmas :)
Address of …Run Code Online (Sandbox Code Playgroud) 假设我有这三个功能:
bool A();
bool B();
bool C();
Run Code Online (Sandbox Code Playgroud)
如何使用函数指针有条件地调用其中一个函数,如何声明函数指针?
我试过这个
typedef void (* __stdcall MessageHandler)(const Task*);
Run Code Online (Sandbox Code Playgroud)
这编译但给了我这个警告(VS2003):
警告C4229:使用的时间错误:忽略数据上的修饰符
我想用stdcall调用约定声明一个指向函数的指针?我究竟做错了什么?
这是这个问题的后续问题:具有任何参数列表的函数的泛型函子
我有这个仿函数类(完整代码见上面的链接):
template<typename... ARGS>
class Foo
{
std::function<void(ARGS...)> m_f;
public:
Foo( std::function<void(ARGS...)> f ) : m_f(f) {}
void operator()(ARGS... args) const { m_f(args...); }
};
Run Code Online (Sandbox Code Playgroud)
在operator()中,我可以使用递归的"剥离"功能轻松访问args ...如http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates
我的问题是:我想在构造函数中访问f的参数类型,即ARGS ....显然我无法访问值,因为到目前为止还没有,但参数类型列表以某种方式埋没在f中,不是吗?
在C中,我需要知道结构的大小,其中包含函数指针.我可以保证在所有平台和架构上:
我认为所有这些答案都是肯定的,但我想确定.对于上下文,我正在呼唤sizeof(struct mystruct),仅此而已.
表达式fun和&fun类型是否相同?
请考虑以下代码:
template <typename Check, typename T>
void check(T)
{
static_assert(is_same<Check, T>::value);
}
void fun()
{}
check<void(*)()>(fun);
check<void(*)()>(&fun);
cout << typeid(fun).name() << endl;
cout << typeid(&fun).name() << endl;
Run Code Online (Sandbox Code Playgroud)
两个断言都成功,这表明两个表达式都具有相同的类型.然而,typeids返回不同的结果:
FvvE
PFvvE
Run Code Online (Sandbox Code Playgroud)
这是为什么?
这是问题所在:
1)我有一个这样的课:
class some_class
{
public:
some_type some_value;
int some_function(double *a, double *b, int c, int d, void *e);
};
Run Code Online (Sandbox Code Playgroud)
2)在里面some_function,我使用some_values从some_class对象得到一个结果.
3)所以,我有一个具体的对象,我想得到一个指向这个对象的指针some_function.
可能吗?我无法使用,some_fcn_ptr因为此函数的结果取决于some_value对象的具体情况.
如何获得指向some_function对象的指针?谢谢.
typedef int (Some_class::*some_fcn_ptr)(double*, double*, int, int, void*);
Run Code Online (Sandbox Code Playgroud)