举一个简单的例子。如果我编译以下main.mm
文件没有ARC,它工作正常。如何使C++模板适用于使用ARC的Objective C类型?
#import <Foundation/Foundation.h>
template <typename T>
int testing(const T & whoCares) {
return 0;
}
int main(int argc, const char * argv[])
{
return testing(@"hello");
}
如果我编译这与ARC,出现以下错误:
/Users/sam/Projects/TemplateTest/TemplateTest/main.mm:10:12: error: no matching function for call to 'testing'
return testing(@"hello");
^~~~~~~
/Users/sam/Projects/TemplateTest/TemplateTest/main.mm:4:5: note: candidate template ignored: substitution failure [with T = NSString *]
int testing(const T & whoCares) {
^
1 error generated.
为什么?更重要的是,它可以解决吗?没有任何进一步的解释为什么替代失败。如果我明确地通过类型,如下所示:
return testing<NSString *>(@"hello");
它的工作原理。说了这么多,我不想,也不应该在我的代码中做这件事。
有趣的是,这只对Objective C类型失败。以下替换做工精细,无论被启用ARC的:
return testing("hello");
return testing(123);
你有没有在您的Xcode版本切换编译器,如果是这样,这是否有所作为的选项? – 2012-07-07 21:21:30
我确实(我正在使用Xcode 4.3.3)。不过,我的印象是,ARC只适用于Apple LLVM Compiler 3.1设置(这是我使用的)。 – SamCee 2012-07-07 22:25:17
您的示例在Clang版本3.2中正常工作。我怀疑@ SamCee的答案是否正确,并且该错误已被修复。 – Quuxplusone 2012-10-09 00:07:56