简体   繁体   中英

Why does'nt the compiler throw error for not returning this pointer for a function whose return type is classname&

Why does'nt the compiler throw error for not returning this pointer for a function whose return type is classname&

Ex: In the functions readonly , readwrite etc , even though we comment return *this and do not return anything , this seems to work fine and also the chaining mechanism is perfectly working in the main function.

Will these functions return *this automatically ?

class OpenFile {
public:
  OpenFile(const std::string& filename);

  OpenFile& readonly();  // changes readonly_ to true
  OpenFile& readwrite(); // changes readonly_ to false
  OpenFile& createIfNotExist();
  OpenFile& blockSize(unsigned nbytes);

private:
  friend class File;
  std::string filename_;
  bool readonly_;          // defaults to false [for example]
  bool createIfNotExist_;  // defaults to false [for example]

  unsigned blockSize_;     // defaults to 4096 [for example]

};
inline OpenFile::OpenFile(const std::string& filename)
  : filename_         (filename)
  , readonly_         (false)
  , createIfNotExist_ (false)
  , blockSize_        (4096u)
{ }
inline OpenFile& OpenFile::readonly()
{ readonly_ = true; return *this; }
inline OpenFile& OpenFile::readwrite()
{ readonly_ = false; return *this; }
inline OpenFile& OpenFile::createIfNotExist()
{ createIfNotExist_ = true; return *this; }
inline OpenFile& OpenFile::blockSize(unsigned nbytes)
{ blockSize_ = nbytes; return *this; }

main()
{
//OpenFile *obj = new OpenFile();
  OpenFile f = OpenFile("foo.txt")
       .readonly()
       .createIfNotExist()
       .blockSize(1024);
}

Deleting return statement will not effect the code at all, Something like this

inline OpenFile& OpenFile::readonly()
{ readonly_ = true;  }
inline OpenFile& OpenFile::readwrite()
{ readonly_ = false;  }
inline OpenFile& OpenFile::createIfNotExist()
{ createIfNotExist_ = true; }
inline OpenFile& OpenFile::blockSize(unsigned nbytes)
{ blockSize_ = nbytes;  }

How is the compiler treating these functions or is it automatically returning *this by looking at the return type?

How is the compiler treating these functions

in an undefined way

or is it automatically returning *this by looking at the return type?

no. the program's behaviour is not predictable and certainly won't be correct.

but it seems to work

in an unoptimised build, very likely. The register holding the this pointer is probably not being modified.

Wait until the optimiser starts inlining your code in your release build. Enjoy the fireworks.

Why doesn't the compiler complain?

If you enable warnings, it will. You should always enable all warnings. They help you to avoid incorrect code:

/tmp/gcc-explorer-compiler116311-75-17mquww/example.cpp: In member function 'OpenFile& OpenFile::readonly()':
28 : warning: no return statement in function returning non-void [-Wreturn-type]
{ readonly_ = true; }
^
Compiled ok

From §6.6.3 (2):

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM