Is this code legal? It compiles but I'm wondering what happens with the return value. Undefined behavior?
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
Is this code legal? It compiles but I'm wondering what happens with the return value. Undefined behavior?
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
The code is legal. auto
deduces to void
and a void
function can return another void
function. A void
function can even
return static_cast<void>("I'm a void");
It's legal, but you can't, for example, assign the result to a variable. [1]
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
int main() {
Foo foo;
auto x = foo.test(); //<---compile error here
}
void x = whatever();
anyway [?]