c++return-valuemost-vexing-parse

Return instance by value from a method stack compilation


Can you explain why this won't compile:

(this is the error:

../Man.cpp:33:9: error: conversion from ‘Man (*)()’ to non-scalar type ‘Man’ requested)

Code:

Man goo(){
  Man m();
  return m;
}

but this does:

Man goo(){
    return Man();
}

Solution

  • Man m();
    

    This means "somewhere else in the program, I will define a function named m that takes no arguments and returns a Man". Yes, even when you write it inside another function.

    Man m;
    

    This means "m is a variable of type Man". Since Man is a class type, the default constructor will be called and no parentheses are necessary.