些細な例を挙げてみましょう。次のmain.mm
ファイルを ARC なしでコンパイルすると、正常に動作します。
#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);