怎样vc2015 +支持运算符隐式转换与decltype(*)?

时间:2017-07-16 10:50:31

标签: c++ c++11 decltype

当我用https://stackoverflow.com/a/3351627/4423987测试代码时,我发现代码无法通过vc2015和vc2017成功编译。同时,clang 3.8.0和g ++ 5.4.0工作正常。

   #include <functional>
#include <iostream>

namespace detail{
template<typename Lambda, typename Func>
struct lambda_wrapper;


template <typename Lambda, typename C, typename R>
struct lambda_wrapper<Lambda,R (C::*)(void) const>{
    static R call(void){
        Lambda x;
        return x();
    }
     //typedef decltype(&call) function_type;  this line of code works fine under g++ and clang but vc2015+
    using function_type= R (*)(void); 
    operator function_type (void) const { return &call;}
};

 template <typename Lambda, typename C, typename R,
                typename A0>
    struct lambda_wrapper<Lambda, R (C::*)(A0) const>
    {
        static R call(A0&& p0)
        {
             Lambda x;
            return x(std::forward<A0>(p0));
        }

    //typedef decltype(&call) function_type;  this line of code works fine under g++ and clang but vc2015+
    using function_type= R (*)(A0&&); 
    operator function_type (void) const { return &call;}
    };
}//end of namespace detail
template <typename Lambda>
struct lambda_wrapper
    :detail::lambda_wrapper<Lambda, decltype(&Lambda::operator())>{};

template <typename Lambda>
lambda_wrapper<Lambda> wrap_lambda(const Lambda&){
    return lambda_wrapper<Lambda>();
}
int main(void)
{
    typedef void(*Ptr)();
    auto l = [](){ std::cout << "im broked :(" << std::endl; };
  //Ptr f=l;
    Ptr f=wrap_lambda(l);
    f();

    typedef void(*Ptr1)(int i);
    auto l1 = [](int i){ std::cout << "im happy :)" << i<<std::endl; };
   //Ptr1 f1=l1;
    Ptr1 f1=wrap_lambda(l1);
    f1(3);
}

在vc2015下编译出错如下:

source_file.cpp(54):错误C2440:'初始化':无法转换为'lambda_wrapper&gt;'到'Ptr1' source_file.cpp(54):注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符 用于x64的Microsoft(R)C / C ++优化编译器版本19.00.23506

**

vc2015 +出了什么问题?

**

  

感谢VTT。没有para的lambda的转换是好的,具有para的lambda的转换怎么样?示例代码已打开   http://rextester.com/MUTT25014

0 个答案:

没有答案