편집 기록

편집 기록
  • 프로필 허대영(소프트웨어융합대학)님의 편집
    날짜2016.08.22

    C++ 템플릿 람다함수가 인자로 전달이 안됩니다.


    //
    //  main.cpp
    //  test
    //
    //  Created by bodguy on 2016. 8. 22..
    //  Copyright © 2016년 bodguy. All rights reserved.
    //
    
    #include <iostream>
    #include <functional>
    #include <type_traits>
    
    template<typename T, typename = std::enable_if_t<std::is_integral<T>::value || std::is_floating_point<T>::value > >
    void add(T p, const std::function<bool(T,T)>& func) {
        std::cout << std::boolalpha;
        std::cout << func(p,p+1) << std::endl;
    }
    
    int main(int argc, const char * argv[]) {
        const std::function<bool(float,float)>& f = [](float a, float b){return a < b;};
        add(10.2f, f);
        // 아래거는 안되는이유??
        // add(10.2f, [](float a, float b){return a < b;});
        return 0;
    }
    

    위의 코드에서 add(10.2f, f); 는 제대로 호출이 되는데 왜 람다식을 인자로 바로 전달했을시에는 컴파일 오류가 나는 걸까요??

  • 프로필 알 수 없는 사용자님의 편집
    날짜2016.08.22

    C++ 템플릿 람다함수가 인자로 전달이 안됩니다.


    //
    //  main.cpp
    //  test
    //
    //  Created by bodguy on 2016. 8. 22..
    //  Copyright © 2016년 bodguy. All rights reserved.
    //
    
    #include <iostream>
    #include <functional>
    #include <type_traits>
    
    template<typename T, typename = std::enable_if_t<std::is_integral<T>::value || std::is_floating_point<T>::value > >
    void add(T p, const std::function<bool(T,T)>& func) {
        std::cout << std::boolalpha;
        std::cout << func(p,p+1) << std::endl;
    }
    
    int main(int argc, const char * argv[]) {
        const std::function<bool(float,float)>& f = [](float a, float b){return a < b;};
        add(10.2f, f);
        // 아래거는 안되는이유??
        // add(10.2f, [](float a, float b){return a < b;});
        return 0;
    }
    

    위의 코드에서 add(10.2f, f); 는 제대로 호출이 되는데 왜 람다식을 인자로 바로 전달했을시에는 컴파일 오류가 나는 걸까요??