local-class

Why generic lambdas are allowed while nested structs with templated methods aren't?

∥☆過路亽.° 提交于 2020-01-30 04:04:31
问题 As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator() . This makes generic lambda very powerful and easy to use tool. On the other hand one can create structs nested into the function, when however the struct has templated member e.g.: #include <iostream> int main() { struct inner { template <class T> void operator()(T &&i) { } }; return 0; } or is templated by itself: int main() { template <class T> struct inner { void operator

Local classes : C++03 vs. C++11

点点圈 提交于 2019-12-18 14:05:42
问题 Is there any change in the usage of local class in C++11? It seems in C++03 local classes cannot be used as template argument (I recall that). Consider this code, template<typename T> void f(const T&) {} //Note : S is a local class defined inside main() int main() { struct S{}; f(S()); } //I want template argument to be deduced. But it gives compilation error (C++03 mode), saying (ideone): prog.cpp:4: error: no matching function for call to ‘f(main()::S)’ However, it compiles fine when

How to use local classes with templates?

萝らか妹 提交于 2019-12-17 20:54:40
问题 GCC doesn't seem to approve of instanciating templates with local classes: template <typename T> void f(T); void g() { struct s {}; f(s()); // error: no matching function for call to 'f(g()::s)' } VC doesn't complain. How should it be done? 回答1: In C++03 it can't be done, C++0x will lift that restriction though. C++03, §14.3.1/2 : A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type

Access problem in local class

别来无恙 提交于 2019-12-13 13:05:30
问题 void foobar(){ int local; static int value; class access{ void foo(){ local = 5; /* <-- Error here */ value = 10; } }bar; } void main(){ foobar(); } Why doesn't access to local inside foo() compile? OTOH I can easily access and modify the static variable value . 回答1: Inside a local class you cannot use/access auto variables from the enclosing scope. You can use only static variables, extern variables, types, enums and functions from the enclosing scope. 回答2: From standard docs Sec 9.8.1 , A

Why can't a struct defined inside a function be used as functor to std::for_each?

好久不见. 提交于 2019-12-10 04:12:08
问题 The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so? #include <map> #include <algorithm> struct Element { void flip() {} }; void flip_all(std::map<Element*, Element*> input) { struct FlipFunctor { void operator() (std::pair<Element* const, Element*>& item) { item.second->flip(); } }; std::for_each(input.begin(), input.end(), FlipFunctor()); } When I move struct FlipFunctor before function flip_all , the code compiles. Full

static member variable inside a local class in c++?

橙三吉。 提交于 2019-12-09 18:41:49
问题 I know we cannot declare a static member variable inside a local class... but the reason for it is not clear. So please can anybody explain it? Also, why can't we access a non-static variable define inside the function, within which the local class has been defined,directly in the local class member functions? In the code given below: int main(int argc, char *argv[]) { static size_t staticValue = 0; class Local { int d_argc; // non-static data members OK public: enum // enums OK { value = 5 }

Why can't a struct defined inside a function be used as functor to std::for_each?

大兔子大兔子 提交于 2019-12-05 04:28:16
The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so? #include <map> #include <algorithm> struct Element { void flip() {} }; void flip_all(std::map<Element*, Element*> input) { struct FlipFunctor { void operator() (std::pair<Element* const, Element*>& item) { item.second->flip(); } }; std::for_each(input.begin(), input.end(), FlipFunctor()); } When I move struct FlipFunctor before function flip_all , the code compiles. Full error message: no matching function for call to ‘for_each(std::_Rb_tree_iterator<std::pair<Element*

C++ can local class reference be passed to a function?

谁说胖子不能爱 提交于 2019-12-04 03:57:00
问题 I would like to know if the following is allowed: template < class C > void function(C&); void function() { class {} local; function(local); } thanks 回答1: It's not allowed right now. But it's supported in C++0x. The current Standard says at 14.3.1/2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter. That said, if the function is also local, there's no problem void f() {

C++ can local class reference be passed to a function?

自作多情 提交于 2019-12-01 18:33:53
I would like to know if the following is allowed: template < class C > void function(C&); void function() { class {} local; function(local); } thanks It's not allowed right now. But it's supported in C++0x. The current Standard says at 14.3.1/2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter. That said, if the function is also local, there's no problem void f() { class L {} local; struct C { static void function(L &l) { // ... } }; C::function(local); } It's allowed if you

Why generic lambdas are allowed while nested structs with templated methods aren't?

青春壹個敷衍的年華 提交于 2019-12-01 15:35:44
As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator() . This makes generic lambda very powerful and easy to use tool. On the other hand one can create structs nested into the function, when however the struct has templated member e.g.: #include <iostream> int main() { struct inner { template <class T> void operator()(T &&i) { } }; return 0; } or is templated by itself: int main() { template <class T> struct inner { void operator()(T &&i) { } }; return 0; } compiler seems to have a problem with compiling it: error: invalid