【发布时间】:2014-09-13 21:33:49
【问题描述】:
以下简化的情况将在 MSVS 13 中编译并运行良好,但使用 gcc 4.9.0 时出现错误:
无法从
<brace-enclosed initializer list>转换为std::vector(std::function<bool(std::string)>>。
#include <iostream>
#include <functional>
#include <vector>
#include <string>
template<typename F> class Foo
{
public:
Foo(int a) : wombat(a) {};
~Foo() {}
bool get_result() { return result; }
protected:
template<typename A> bool do_something(std::string& s, A& a, A b, A c);
bool result;
int wombat;
};
template<typename F> template<typename A> bool Foo<F>::do_something(std::string& s, A& a, A b, A c)
{
if ( a > b && a < c)
{
std::cout << s << std::endl;
return true;
}
return false;
}
struct Tim
{
int age;
float weight;
};
class Bar : public Foo<Tim>
{
public:
Bar(int a) : Foo<Tim>(a) {};
~Bar() {};
void do_somethings();
};
void Bar::do_somethings()
{
Tim t;
t.age = 15;
std::vector<std::function<bool(std::string)> > my_list = {
std::bind(&Bar::do_something<int>, this, std::placeholders::_1, std::ref(t.age), 10, 100)
}; // Error shows up here
for( auto v : my_list) { result = v("howdy"); }
}
int main(int argc, char** argv)
{
Bar b(200);
b.do_somethings();
return 0;
}
我是不是做错了什么,或者错过了初始化列表应该如何工作的一些内容?
【问题讨论】:
-
@Praetorian,抱歉,我安装编译器的计算机没有连接到互联网,所以我必须手动重新输入示例。
-
我不认为这是一个最小的例子。或附近的任何地方。
标签: c++ visual-c++ gcc c++11 initializer-list