local-class

Why aren't static data members allowed in local classes?

旧时模样 提交于 2019-11-30 21:36:10
问题 What is the reasoning to why static const members cannot exist in local classes? It seems like a rather silly restriction. Example: void foo() { struct bar { int baz() { return 0; } // allowed static const int qux = 0; // not allowed?!? }; } struct non_local_bar { int baz() { return 0; } // allowed static const int qux = 0; // allowed }; Quote from standard (9.8.4): A local class shall not have static data members. 回答1: From the standard section 9.4.2: If a static data member is of const

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

与世无争的帅哥 提交于 2019-11-30 11:02:37
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 compiling it in C++11 mode ( ideone ), which makes sense to me, otherwise lambda wouldn't work. So I guess

Member template in local class

天涯浪子 提交于 2019-11-29 05:23:59
Given the following code: void f() { class A { template <typename T> void g() {} }; } g++ 4.4 (and also g++-4.6 -std=gnu++0x ) complains: "invalid declaration of member template in local class". Apparently local classes are not allowed to have template members. What is the purpose of this limitation? Will it be removed in C++0x? Note: If I make the local class itself a template, rather than giving it a template member: void f() { template <typename T> class A { void g() {} }; } I get "error: a template declaration cannot appear at block scope". The purpose of this limitation? Just a guess, but

How to use local classes with templates?

人走茶凉 提交于 2019-11-28 14:00:14
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? 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-parameter. 来源: https://stackoverflow.com/questions/3172909/how-to-use-local-classes-with-templates

Local class can access non-final variable in java 8

久未见 提交于 2019-11-28 11:57:21
Before Java 8, We were not able to use non-final variables inside local class. But now they are allowing final as well as effectively final(who's values has not been changed), can be referred by local classes. What i know(Correct me if i am wrong), they didn't supported referring non-final values because there values can be changed. So, how they are supporting it now and why it was not supported earlier. The situation has not changed at all, actually. The compiler is just a bit smarter, and doesn't force you to use the final keyword anymore. If it detects that the variable is effectively final

Member template in local class

爷,独闯天下 提交于 2019-11-27 22:53:54
问题 Given the following code: void f() { class A { template <typename T> void g() {} }; } g++ 4.4 (and also g++-4.6 -std=gnu++0x ) complains: "invalid declaration of member template in local class". Apparently local classes are not allowed to have template members. What is the purpose of this limitation? Will it be removed in C++0x? Note: If I make the local class itself a template, rather than giving it a template member: void f() { template <typename T> class A { void g() {} }; } I get "error:

Local class can access non-final variable in java 8

会有一股神秘感。 提交于 2019-11-27 06:38:07
问题 Before Java 8, We were not able to use non-final variables inside local class. But now they are allowing final as well as effectively final(who's values has not been changed), can be referred by local classes. What i know(Correct me if i am wrong), they didn't supported referring non-final values because there values can be changed. So, how they are supporting it now and why it was not supported earlier. 回答1: The situation has not changed at all, actually. The compiler is just a bit smarter,

Use of class definitions inside a method in Java

坚强是说给别人听的谎言 提交于 2019-11-26 11:04:39
Example: public class TestClass { public static void main(String[] args) { TestClass t = new TestClass(); } private static void testMethod() { abstract class TestMethod { int a; int b; int c; abstract void implementMe(); } class DummyClass extends TestMethod { void implementMe() {} } DummyClass dummy = new DummyClass(); } } I found out that the above piece of code is perfectly legal in Java. I have the following questions. What is the use of ever having a class definition inside a method? Will a class file be generated for DummyClass It's hard for me to imagine this concept in an Object

Use of class definitions inside a method in Java

∥☆過路亽.° 提交于 2019-11-26 02:28:48
问题 Example: public class TestClass { public static void main(String[] args) { TestClass t = new TestClass(); } private static void testMethod() { abstract class TestMethod { int a; int b; int c; abstract void implementMe(); } class DummyClass extends TestMethod { void implementMe() {} } DummyClass dummy = new DummyClass(); } } I found out that the above piece of code is perfectly legal in Java. I have the following questions. What is the use of ever having a class definition inside a method?