c++ - 自动返回类型匹配 void

标签 c++ c++11 c++14

这段代码合法吗?它可以编译,但我想知道返回值会发生什么。未定义的行为?

class Foo {
public:
    void test1() {

    }
    auto test() -> decltype(test1()) {
        return test1(); //<---return void here!
    }
};

最佳答案

该代码是合法的。 auto 推导出 void 并且 void 函数可以返回另一个 void 函数。 void 函数甚至可以

return static_cast<void>("I'm a void");

关于c++ - 自动返回类型匹配 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59542181/

相关文章:

c++ - 在 valgrind 中运行程序时连接超时

c++ - Object b(); 有什么区别?和对象 b;?

c++ - 类成员初始化是在编译时还是运行时进行?

c++ - 将 integral_constants 的元组转换为 int 的 constexpr 元组

c++ - ~A(){} 和 ~A() throw(){} 有什么区别,其中 A 是类名?

c# - 从 C++ 调用 C# 函数是否创建编译代码

c++ - 为什么编译器不针对同一翻译单元中的 ODR 违规发出警告

c++ - 转发初始化列表表达式

c++ - 当 decltype 应用于它们时,哪些表达式会产生引用类型?

c++ - 实现 `to_xstring()` 来合并 `to_string()` 和 `to_wstring()`