簡體   English   中英

使用 IMPLICIT 實例化而不是 .H 允許在 CPP 文件中放置類成員函數的 C++ 模板定義?

[英]Placing C++ template definition for a class member function in CPP file with IMPLICIT instantiation instead of .H allowed?

給定的代碼如下:

文件

#include"foo.h"

template <int X>
void bar::foo(){
 cout << X << endl;
}

static void tea(){
 bar().foo<1>();
 bar().foo<2>();
 bar().foo<3>();
}

foo.h

class bar {
 public:
  template <int X>
  void foo();
};

主程序

...
bar().foo<1>();
bar().foo<2>();
bar().foo<3>();
...

任何版本的 C++ 都允許這種模式嗎?

編輯:

當使用template void bar::foo<1>() (等)顯式實例化允許正確鏈接時,為什么不允許這樣做?

C++ 編譯器不需要為任何東西實際生成符號,除非你用 extern 聲明強制它。 他們積極地內聯函數,這可能會刪除您實例化的所有證據。 我嘗試了你的例子,它與-O0相關聯,但與-O3無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM