2017-12-28 7 views
0

이 특정 코드는 gcc 5.4에서는 실패하지만 다른 여러 버전, msvc 2015, clang 및 최신 버전의 gcc에서 작동합니다.함수 변환을 높이기위한 익명 구조체가 gcc 5.4에서 실패합니다.

#include <boost/function.hpp> 

#include <map> 
#include <string> 

typedef boost::function1<int, double> fn; 

class fn_map : public std::map<std::string, fn> { 
public: 
    void reg(const std::string&, fn); 
}; 

int main(int, char**) { 
    fn_map fm; 

    struct { 
     int operator()(double v) { 
      return static_cast<int>(v * 2.); 
     } 
    } factory; 

    fm.reg("factory", factory); 
} 

void fn_map::reg(const std::string& nm, fn f) { 
    this->insert(std::make_pair(nm, f)); 
} 

오류 메시지 :

no known conversion for argument 2 from 'main(int, char**)::<anonymous struct>' to 'fn {aka boost::function1<int, double>}' 

나뿐만 아니라이 작업이 코드를 가지고 싶습니다 가능하다면, 그래서 LTS는 여전히 GCC (5)이 마지막 우분투.

답변

0

함수 정의에서 struct 정의를 익명이 아닌 유형으로 이동하면 gcc에서이 특정 문제를 해결할 수 있습니다.

#include <boost/function.hpp> 

#include <map> 
#include <string> 

typedef boost::function1<int, double> fn; 

class fn_map : public std::map<std::string, fn> { 
public: 
    void reg(const std::string&, fn); 
}; 

namespace { 
    struct factory_t { 
     int operator()(double v) { 
      return static_cast<int>(v * 2.); 
     } 
    }; 
} 

int main(int, char**) { 
    fn_map fm; 

    factory_t factory; 

    fm.reg("factory", factory); 
} 

void fn_map::reg(const std::string& nm, fn f) { 
    this->insert(std::make_pair(nm, f)); 
}