Objective-C ++编译器为C ++ lambda语法提供了多个消息参数?

时间:2016-08-05 08:03:44

标签: c++ gcc lambda objective-c++ gnustep

据我所知,以下应该是合法的Objective-C ++。但它没有编译:

/*
 * compile:
 *
 * gcc -c $(gnustep-config --objc-flags) -x objective-c++
 *
 * fails for gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.1)
 *
 * works with -DNO_SECOND or -x objective-c
 */

#import <Foundation/Foundation.h>

/*
 * if defined, use only one argument
 */
#ifdef NO_SECOND
#   define X(_)
#else
#   define X(_) _
#endif

@interface Foo : NSObject
@end

@implementation Foo : NSObject
int a0, a1;
-(id)initWith: (int)a0_in X(: (int)a1_in) {
    a0 = a0_in;
    X(a1 = a1_in;)
    return [super init];
}
-(int)a0 {return a0;}
X(-(int)a1 {return a1;})
@end

int main() {
    int a0 = 0;
    X(int a1 = 1;)
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    id bar = [Foo alloc];
    bar = [bar initWith:a0 X(:a1)];
    NSLog(@"%i", [bar a0]);
    X(NSLog(@"%i", [bar a1]);)
    [pool drain];
    return 0;
}

问题似乎是发送给initWith的邮件bar。编译器抱怨:

m.mm: In function ‘int main()’:
m.mm:46:27: error: found ‘:’ in nested-name-specifier, expected ‘::’
  bar = [bar initWith:a0 X(:a1)];
                           ^
m.mm:24:15: note: in definition of macro ‘X’
 # define X(_) _
               ^
m.mm:46:22: warning: no ‘-initWith:’ method found
  bar = [bar initWith:a0 X(:a1)];
                      ^
m.mm:46:22: warning: (Messages without a matching method signature
m.mm:46:22: warning: will be assumed to return ‘id’ and accept
m.mm:46:22: warning: ‘...’ as arguments.)
m.mm:46:13: error: expected ‘,’ before ‘initWith’
  bar = [bar initWith:a0 X(:a1)];
             ^
m.mm:46:13: error: ‘initWith’ was not declared in this scope
m.mm:46:21: error: expected ‘,’ before ‘:’ token
  bar = [bar initWith:a0 X(:a1)];
                     ^
m.mm:46:21: error: expected identifier before ‘:’ token
m.mm: In lambda function:
m.mm:46:32: error: expected ‘{’ before ‘;’ token
  bar = [bar initWith:a0 X(:a1)];
                                ^
m.mm: In function ‘int main()’:
m.mm:46:32: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
m.mm:46:6: error: cannot convert ‘main()::<lambda()>’ to ‘objc_object*’ in assignment
  bar = [bar initWith:a0 X(:a1)];
      ^
m.mm:43:8: warning: unused variable ‘a1’ [-Wunused-variable]
  X(int a1 = 1;)
        ^
m.mm:24:15: note: in definition of macro ‘X’
 # define X(_) _
               ^

不知何故,编译器认为这是一个C ++ lambda。但是,如果initWith只有一个参数(使用-DNO_SECOND尝试此操作),则代码可以正常工作。

请注意,此代码被接受为Objective-C代码:使用-x objective-c进行编译。与$(gnustep-config --base-libs)相关联。运行并阅读:

2016-08-05 10:59:36.251 m[21666] 0
2016-08-05 10:59:36.252 m[21666] 1

我做错了什么?或者Objective-C ++被破坏了,无法修复?

0 个答案:

没有答案