Linux C ++:如何在多個文件中正確使用模板特化?

[英]Linux C++: How to properly use template specializations across multiple files?


I have a strange problem. On Windows, with Visual Studio 2010 and also with the Intel Compiler everything is linked as expected. But when I try to compile my code with CLang 3.0 on Linux, it does compile (and if I only use a single CPP file it does also link and run) but does not link.

我有一個奇怪的問題。在Windows上,使用Visual Studio 2010以及英特爾編譯器,所有內容都按預期鏈接。但是當我嘗試在Linux上使用CLang 3.0編譯我的代碼時,它確實編譯(如果我只使用單個CPP文件,它也鏈接和運行)但不鏈接。

The message is that there are multiple symbol definitions, referring to template instanciations. For example consider the following two lines in a header file shared across multiple compilation units:

消息是有多個符號定義,指的是模板實例化。例如,在多個編譯單元共享的頭文件中考慮以下兩行:

 template<class T> void myFunc(T in) { }
 template<> void myFunc<int>(int in) { }

Now from the Linux linker I would get something along the lines of:

現在從Linux鏈接器我會得到以下內容:

"file xyz": Multiple definition of "myFunc(int in)", first defined in "some file".

“file xyz”:“myFunc(int in)”的多個定義,首先在“some file”中定義。

But how would I prevent that? Since it works on Windows I suppose it should work on Linux too somehow?

但是我該怎么做呢?既然它適用於Windows,我認為它應該在某種程度上適用於Linux?

The same goes for static template data members, which are more or less the same as above just that you declare a variable instead of a function. I would prefer if it worked for static template data members.

靜態模板數據成員也是如此,這些成員或多或少與上面相同,只是您聲明了一個變量而不是一個函數。我更喜歡它是否適用於靜態模板數據成員。

If everything else fails I suppose I could still create a "MakeAll.cpp" file which just includes all CPP there are, but that doesn't sound like a desirable solution to me...

如果其他一切都失敗了,我想我仍然可以創建一個“MakeAll.cpp”文件,其中包含所有CPP,但這對我來說聽起來不是一個理想的解決方案......

Thanks for your help!

謝謝你的幫助!

3 个解决方案

#1


9  

In my understanding, you are in fact defining your template specializations multiple times, and this should also give you an error for Windows compilers.

根據我的理解,您實際上是多次定義模板特化,這也應該為Windows編譯器提供錯誤。

In your header file you are defining a function by providing a body:

在頭文件中,您通過提供正文來定義函數:

template<> void myFunc<int>(int in) { }

This definition will exist in multiple compilation units and the linker should complain.

該定義將存在於多個編譯單元中,並且鏈接器應該抱怨。

The same rules apply for your template specialization as for ordinary non-template functions: Either use inline or use a separate declaration and definition, by putting

與普通的非模板函數相同的規則適用於您的模板專業化:使用內聯或使用單獨的聲明和定義,通過放置

template<> void myFunc<int>(int in);

in a header and

在標題和

template<> void myFunc<int>(int in)
{
    // ...
}

in a .cpp file.

在.cpp文件中。

#2


3  

Templates are instantiated by the compiler, and it is the compilers responsibility to make sure that they are only defined once.

模板由編譯器實例化,編譯器有責任確保它們僅定義一次。

When you fully specialize a function, it is not a template anymore (but an ordinary function), and it is your responsibility to make sure it is not multiply defined.

當你完全專門化一個函數時,它不再是一個模板(而是一個普通的函數),你有責任確保它不是多重定義的。

There is very little difference between these functions

這些功能之間的差別很小

template<>
void f<int>(int x)
{ }

void f(int x)
{ }

when it comes to the one definition rule.

當談到一個定義規則。

Adding inline helps in both cases.

在兩種情況下添加內聯都有幫助。

#3


0  

I don't have the standard at hand now, but I think that the specialization has to be declared inline.

我現在手頭沒有標准,但我認為專業化必須在線聲明。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/23/72f4cb6bb11f0ea5375df0f976d80c91.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com