我想将一个void*转换为char*reinterpret_cast和static_cast,哪一个适合?static_cast<char*> or reinterpret_cast<char*>
我在linux上使用GCC 4.1.2,STL必须是SGI STL.我写完之后:
#include <functional>
std::find_if(PirateFlush, PirateFlush + size,
compose2(std::logical_and<bool>(), bind2nd(std::greater<UInt32>(), time),
bind2nd(std::less<UInt32>(), now)));
Run Code Online (Sandbox Code Playgroud)
编译说:
错误:'compose2'未在此范围内声明
怎么了?
#include <iostream>
#include <string>
using namespace std;
struct Uid {
typedef int type;
};
struct Name {
typedef string type;
};
struct Age {
typedef int type;
};
template <class T1, class T2, class T3>
class People {
private:
typename T1::type val1;
typename T2::type val2;
typename T3::type val3;
//add a get function here
}
};
int main() {
People<Uid, Name, Age> people;
people.get<Uid>(); //make this validate
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码,我想在类中添加一个get函数,使函数调用get in main validate.我尝试在类中添加一个tempalte get及其特化版本,但它是一个无效的方法,编译器说:非命名空间范围'class People'中的显式特化.有人说这种方法适用于vs,但它打破了标准.
在许多脚本语言中,我们有一个这样的编程方法:
首先,有一个名为func的函数:
void func()
{
}
Run Code Online (Sandbox Code Playgroud)
其次,我想在客户端调用此函数时记录一些信息,但我不想修改函数,所以我可以这样做:
void (*pfunc)(void) = func;
void func()
{
log("Someone call fund");
pfunc();
}
Run Code Online (Sandbox Code Playgroud)
之后,任何拨打电话的人都会调用我的"覆盖"功能.这在许多脚本语言中都可以.我可以用C语言做同样的事情吗?以及如何编码?
我想用这个方法在一些3party库中做一些工作,所以我必须做一些影响链接过程的事情,而不仅仅是编译过程.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void push_back(int v, vector<int>& coll)
{
coll.push_back(v);
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
std::vector<int> b;
for_each(a, a + 5, bind2nd(ptr_fun(push_back), b));
}
Run Code Online (Sandbox Code Playgroud)
编译说:
/usr/include/c++/4.1.2/bits/stl_function.h: In instantiation of ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >’:
tt5.cpp:15: instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:435: error: forming reference to reference type ‘std::vector<int, std::allocator<int> >&’
/usr/include/c++/4.1.2/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>, _Tp = …
Run Code Online (Sandbox Code Playgroud)