Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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_Crtp_Return Type Deduction - Fatal编程技术网

C++ 为什么自动退货类型扣除适用于未完全定义的类型?

C++ 为什么自动退货类型扣除适用于未完全定义的类型?,c++,c++14,crtp,return-type-deduction,C++,C++14,Crtp,Return Type Deduction,考虑以下几点: template<typename Der> struct Base { // NOTE: if I replace the decltype(...) below with auto, code compiles decltype(&Der::operator()) getCallOperator() const { return &Der::operator(); } }; struct Foo : Bas

考虑以下几点:

template<typename Der>
struct Base {
    // NOTE: if I replace the decltype(...) below with auto, code compiles
    decltype(&Der::operator()) getCallOperator() const {
        return &Der::operator();
    }
};

struct Foo : Base<Foo> {
    double operator()(int, int) const {
        return 0.0;
    }
};

int main() {
    Foo f;
    auto callOp = f.getCallOperator();
}
模板
结构基{
//注意:如果我将下面的decltype(…)替换为auto,代码将编译
decltype(&Der::operator())getCallOperator()常量{
return&Der::operator();
}
};
结构Foo:Base{
双运算符()(int,int)常量{
返回0.0;
}
};
int main(){
福福;
auto callOp=f.getCallOperator();
}
我想在CRTP基类中创建一个成员函数,其返回类型取决于派生类中
操作符()的签名。但是
decltype(&Der::operator())
无法编译;
Foo
中的
operator()
成员函数不可见。我假设这是因为基类模板是在
Foo
完全定义之前实例化的

令人惊讶的是,如果我为它编译的返回类型放置
auto
。我假设
auto
将使编译器从函数体推断返回类型并失败-因为函数体使用未完全定义的
Foo
类型

MSVC 2015.3和Clang 3.8的行为相同


为什么代码开始使用
auto
auto
类型推断是否以某种方式“延迟”了实例化?或者使用与手写返回类型表达式不同的上下文?

您的猜测是正确的。在需要函数签名之前,推导出的返回类型实际上不会被推导出来。这意味着它将在调用
getCallOperator
的上下文中推导,此时
Foo
已完全定义

这在7.1.6.4p12中有规定:

在实例化定义时,即使函数体包含具有非类型依赖操作数的返回语句,也会对声明类型中包含占位符的函数模板进行返回类型推断


好问题。投票结果向上。它可能重复同一个问题,但问题和答案的角度略有不同。这里我们有“自动演绎有什么不同”,链接的问题及其答案更多的是关于“我怎样才能使手写表达有效”。虽然它可能仍然有资格作为复制品…