C ++ can pass a local class reference to a function?

I would like to know if the following is allowed:

template < class C > void function(C&); void function() { class {} local; function(local); } 

thanks

+4
source share
2 answers

This is not allowed right now. But it is supported in C ++ 0x. Current standard says 14.3.1/2

A local type, a type without a binding, an unnamed type, or a type made up of any of these types should not be used as a template argument for a template type parameter.

However, if the function is also local, there is no problem

 void f() { class L {} local; struct C { static void function(L &l) { // ... } }; C::function(local); } 
+6
source

This is allowed if you use polymorphism instead of patterns. Or, if you do not need to extend the interface visible with function , simple inheritance will be performed.

 void function( ABC & ); void function() { class special : public ABC { virtual void moof() {} } local; function(local); } 
0
source

Source: https://habr.com/ru/post/1307278/


All Articles