Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 方法定义的类型不为';不匹配声明_C++_C++14_Auto_Decltype_Type Deduction - Fatal编程技术网

C++ 方法定义的类型不为';不匹配声明

C++ 方法定义的类型不为';不匹配声明,c++,c++14,auto,decltype,type-deduction,C++,C++14,Auto,Decltype,Type Deduction,我试图使用C++11/14的一些新特性,但在它们的定义中遇到了类方法类型推断的麻烦 情景: // in header foo.hpp class MyClass { T foo(); } //in source foo.cpp auto MyClass::foo() { return ... //something that returns T! } 对于T=cl_uint(OpenCL),这不起作用,编译器输出以下错误消息: src/device.cpp:9:7:错误:“a

我试图使用C++11/14的一些新特性,但在它们的定义中遇到了类方法类型推断的麻烦

情景:

// in header foo.hpp
class MyClass {
    T foo();
}

//in source foo.cpp
auto MyClass::foo() {
    return ... //something that returns T!
}
对于T=cl_uint(OpenCL),这不起作用,编译器输出以下错误消息:

src/device.cpp:9:7:错误:“auto-CL::device::addressBits()const”的原型与类“CL::device”中的任何原型都不匹配

src/device.hpp:31:11:错误:候选项为:cl\u uint cl::device::addressBits()const

这与GCC和Clang的最新版本表现相同。 具体例子如下:

// in the .hpp
namespace CL {
    class Device : public Object<cl_device_id, cl_device_info, DeviceFunctions> {
    public:
        Device(cl_device_id id);

        cl_uint addressBits() const;

        // much more stuff ... (not of interest atm)
    }
}

// in the .cpp
namespace CL {
    auto Device::addressBits() const {
        return getInfo<cl_uint>(CL_DEVICE_ADDRESS_BITS);
    }
}

// in object.hpp => inherited by device
namespace CL {
    template<typename U, typename InfoIdType, typename Functions>
    class Object {
    protected:
        template<typename T>
        T getInfo(InfoIdType info_id) const {
            auto error = cl_int{CL_INVALID_VALUE};
            auto info  = T{};
            error = Functions::get_info(m_id, info_id, sizeof(T), &info, nullptr);
            return (error == CL_SUCCESS) ? info : T{};
        }
    }
}
//在.hpp中
命名空间CL{
类设备:公共对象{
公众:
设备(cl_设备id);
cl_uint addressBits()常量;
//更多的东西…(不感兴趣)
}
}
//在.cpp中
命名空间CL{
自动设备::addressBits()常量{
返回getInfo(CL\u设备\u地址\u位);
}
}
//在object.hpp=>中由设备继承
命名空间CL{
模板
类对象{
受保护的:
模板
T getInfo(InfoIdType info_id)常量{
自动错误=cl_int{cl_无效值};
自动信息=T{};
error=函数::get_info(m_id,info_id,sizeof(T),&info,nullptr);
返回(error==CL_SUCCESS)?信息:T{};
}
}
}
我很清楚,这个问题不会导致任何可怕的事情,也不能通过在这个场景中省略类型推断来解决。 然而,当我尝试采用新的、很酷的C++11/14功能时,我想了解为什么它不能像我想象的那样工作。

以下工作:

struct MyClass {
    auto foo();
};

auto MyClass::foo() {
    return 10;
}
虽然我不知道为什么你的案子不管用


(使用,现在我知道了)

您可以将所有代码简化为:

struct A {
  int func();
};

auto A::func() { return 0; }
这是无效的,使用占位符类型声明的函数必须在所有声明中使用占位符:

[十二月规格自动]/13:

具有使用占位符类型的已声明返回类型的函数或函数模板的重新声明或专门化也应使用该占位符,而不是推断类型

可能重复的