Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disallow temporaries

Tags:

c++

For a class Foo, is there a way to disallow constructing it without giving it a name?

For example:

Foo("hi"); 

And only allow it if you give it a name, like the following?

Foo my_foo("hi"); 

The lifetime of the first one is just the statement, and the second one is the enclosing block. In my use case, Foo is measuring the time between constructor and destructor. Since I never refer to the local variable, I often forget to put it in, and accidentally change the lifetime. I'd like to get a compile time error instead.

like image 315
Martin C. Martin Avatar asked Oct 31 '12 14:10

Martin C. Martin


People also ask

Is it OK to delete temp files?

If you're running low on storage space, you should consider deleting the temp files. You can either delete some or all of the temp files. Deleting them will free up space that you can use for other files and data. Keep in mind that you may not be able to delete temp files while the respective program is still running.

Can we delete temporary files Disk Cleanup?

To delete temporary files: In the search box on the taskbar, type disk cleanup, and select Disk Cleanup from the list of results. Select the drive you want to clean up, and then select OK. Under Files to delete, select the file types to get rid of.

Is it safe to delete temp files Windows 10?

You can delete them manually or use some third-party software like “CCleaner” to clean it up for you. So, as all mentioned above about the temporary files, there is no need to worry about the temporary files. In most cases, the deleting of temporary files will be automatically done but you can do it yourself too.


1 Answers

Another macro-based solution:

#define Foo class Foo 

The statement Foo("hi"); expands to class Foo("hi");, which is ill-formed; but Foo a("hi") expands to class Foo a("hi"), which is correct.

This has the advantage that it is both source- and binary-compatible with existing (correct) code. (This claim is not entirely correct - please see Johannes Schaub's Comment and ensuing discussion below: "How can you know that it is source compatible with existing code? His friend includes his header and has void f() { int Foo = 0; } which previously compiled fine and now miscompiles! Also, every line that defines a member function of class Foo fails: void class Foo::bar() {}")

like image 131
ecatmur Avatar answered Oct 19 '22 23:10

ecatmur