【问题标题】:C++ Returning Type Deduction FailingC++ 返回类型推导失败
【发布时间】:2014-02-27 14:21:07
【问题描述】:

fmap 函数的返回类型不会自动推断为正确的 Option 类型

谁能帮忙?

#include <iostream>
#include <memory>

template<class T>
class Option{
public:
    Option() : value_(nullptr){
    }
    Option(T const& value) : value_(std::move(value)){

    }
    Option(T&& value) : value_(std::make_shared<T>(std::move(value))){

    }
    Option(Option const&) = default;
    Option(Option&&) = default;
    Option& operator = (Option const&) = default;
    Option& operator = (Option&&) = default;

    template<class U>
    auto fmap(std::function<U ( T const& ) > func) -> Option<decltype( func(std::declval<T>() ) )>{
        return Option<U>(func(*value_));
    }

    template<class U>
    Option<U> operator >>= (std::function<Option<U> ( T const& ) > func){
        if(value_ == nullptr)
            return Option<U>();
        return func(value_);
    }

    static Option<T> Unit(T const& value){
        return Option<T>(value);
    }

    static Option<T> Unit(T&& value){
        return Option<T>(std::move(value));
    }
private:
    std::shared_ptr<T> value_;
};

int main(int argc, const char * argv[])
{


    Option<int> o(55);
    auto f = [](int const& i){
        return i + 1;
    };
    auto result = o.fmap<int>(f);


    return 0;
}

【问题讨论】:

    标签: c++ decltype trailing-return-type


    【解决方案1】:
    #include <iostream>
    #include <memory>
    
    template<class T>
    class Option{
    public:
        Option() : value_(nullptr){
        }
        Option(T const& value) : value_(std::move(value)){
    
        }
        Option(T&& value) : value_(std::make_shared<T>(std::move(value))){
    
        }
        Option(Option const&) = default;
        Option(Option&&) = default;
        Option& operator = (Option const&) = default;
        Option& operator = (Option&&) = default;
    
        template<class Function>
        auto fmap(Function func) -> Option<decltype(func( std::declval<T>() ))>{
            return Option<decltype(func( std::declval<T>() ))>(func(*value_));
        }
    
        template<class Function>
        auto operator >>= (Function func) -> decltype(func( std::declval<T>() )){
            if(value_ == nullptr)
                return decltype(func( std::declval<T>() ))();
            return func(*value_);
        }
    
        void foreach(std::function<void ( T const& ) > func){
            return func(*value_);
        }
    
        static Option<T> Unit(T const& value){
            return Option<T>(value);
        }
    
        static Option<T> Unit(T&& value){
            return Option<T>(std::move(value));
        }
    private:
        std::shared_ptr<T> value_;
    };
    
    int main(int argc, const char * argv[])
    {
    
    
        Option<int> o(55);
        auto f = [](int const& i){
            return i + 1;
        };
    
        auto result = o.fmap(f);
    
        auto tresult = result >>= [](int const& v){
            return Option<int>::Unit(v * 2);
        };
    
        tresult.foreach([](int const& i){
            std::cout << i << std::endl;
        });
    
        return 0;
    }
    

    【讨论】:

    • 如果我可以使用 std::function 会很好
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    相关资源
    最近更新 更多