2016-08-04 43 views
0

我想获得相同的行为,因为这:如何用C++ 03中的成员函数返回的值填充向量?

IdentifiersGenerator gen; 
for(int i = 0; i < 100; ++i) 
    v.push_back(gen.getNextIdentifiers()); 

具有类似的语法:

IdentifiersGenerator gen; 
std::vector<Identifiers> v(100); 
std::generate(v.begin(), v.end(), 
    std::bind1st(std::mem_fun(&IdentifiersGenerator::getNextIdentifiers), gen)); 

上面的代码提供了以下错误:

test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:100: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:103: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:106: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:111: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:117: error: no type named ‘second_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h: In function ‘std::binder1st<_Operation> std::bind1st(const _Operation&, const _Tp&) [with _Operation = std::mem_fun_t<const Identifiers&, IdentifiersGenerator>, _Tp = IdentifiersGenerator]’: 
test/src/IdentifiersGeneratorTest.cpp:449: instantiated from here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/backward/binders.h:126: error: no type named ‘first_argument_type’ in ‘class std::mem_fun_t<const Identifiers&, IdentifiersGenerator>’ 

或许这能够即使没有std::vector默认对象初始化使用例如boost::make_function_input_iterator

EDIT行,所以我已经意识到,我可以使用boost::bind像这样:

std::vector<AlarmIdentifiers> v(100); 
std::generate(v.begin(), v.end(), 
    boost::bind(&IdentifiersGenerator::getNextIdentifiers, &gen)); 

任何人都可以共享的方法来初始化指针成员函数,而不是构建载体中,然后在其内部产生?

+0

是否好做一个仿函数? – NathanOliver

+0

不,我需要成员函数'IdentifiersGenerator :: getNextIdentifiers' – Patryk

+0

你可以使用函数来获得它。我在问你是否可以用一个仿函数包装这个电话。 – NathanOliver

回答

1

的原因,这并不工作:

std::bind1st(std::mem_fun(&IdentifiersGenerator::getNextIdentifiers), gen) 

bind1st需要二进制功能,而你提供一元之一。没有预先包装的C++ 03解决方案,如果boost::bind是我可以使用的一个选项。

否则,可以编写自己的工厂,它需要一个指向无效成员函数的指针和一个指向该类的指针,并返回一个调用它的对象operator()

0

(张贴在这里仅做参考)

好了,我已经意识到我可以使用boost::bind像这样:

std::vector<AlarmIdentifiers> v(100); 
std::generate(v.begin(), v.end(), 
    boost::bind(&IdentifiersGenerator::getNextIdentifiers, &gen)); 
0

绑定是一个真正的“老”,大多过时的方式来实现的东西类似于更好的lambda语法。

IdentifiersGenerator gen; 
std::vector<Identifiers> v(100); 
std::generate(v.begin(), v.end(), [&](){ return gen.getNextIdentifiers(); }); 

你也可以使用generate_n

+0

再次,lambda是C++ 11,我已明确标记此问题为C++ 03 – Patryk

相关问题