2016-10-07 54 views
1

Header.h使用別名的顯式模板實例化?

template <int> 
class FiniteElement 
{ 
public: 
    int GetDOF(); 
}; 

using FiniteElement2D= FiniteElement<3>; 
using FiniteElement3D= FiniteElement<6>; 

Source.cpp

#include "Header.h" 

//template class FiniteElement<3>; 
//template class FiniteElement<6>; 
template FiniteElement2D; // Using alias for explicit template instantiation !!! 
template FiniteElement3D; 

template <int DOF> 
int FiniteElement<DOF>::GetDOF() 
    { return DOF; } 

Main.cpp的

#include "Header.h" 
#include <iostream> 

int main() 
{ 
FiniteElement3D Elem; 

std::cout << Elem.GetDOF(); 

return 0; 
} 

令我驚訝的是,上面的程序編譯,並與視覺鏈接Studio 2015 Update 3.我喜歡允許別名用於顯式模板實例化的想法,但它似乎不適用於gcc或clang。

它是即將到來的標準的特徵還是VS的特定特徵?

+0

這未能與當前的Clang/C2(2016年7月)鏈接,但仍與當今的每日構建(v19.10.24606)鏈接。請[提交錯誤報告](https://connect.microsoft.com/VisualStudio/)並在此處發佈鏈接。 – ildjarn

+1

@GuillaumeRacicot回想起來,很容易稱之爲「愚蠢」,現在很清楚的是一個糟糕的設計決定。 https://blogs.msdn.microsoft.com/vcblog/2015/09/25/rejuvenating-the-microsoft-cc-compiler/是的,VS仍然不符合,是的,所有其他主要編譯器都完全符合C + +14現在一段時間。但是你必須對他們的擁有權和(緩慢)進入正確的方向給予好評:https://blogs.msdn.microsoft.com/vcblog/2016/06/07/expression-sfinae-improvements-in-vs- 2015-update-3 /。他們(以及我們作爲用戶)正在爲很久以前犯的一個錯誤付錢 - **看起來是正確的**。 – bolov

+0

@bolov是的,你是對的。我想我有今天的VS錯誤負載... –

回答

1

答案在評論中給出,但是以一種輕微的變相方式,所以我會在這裏展開它。

MSVC編譯器在這種情況下工作的方式幾乎就像在程序代碼中進行文本替換一樣。它基本上用FiniteElement<3>替換FiniteElement2D的所有文本 - 這種方式顯式實例對你很好。

另一方面,其他編譯器爲typedef構建適當的抽象語法樹,因此別名用法不會擴展爲顯式模板實例化。

作爲一個方面說明,我不知道你期望從你的語法中獲得什麼樣的好處。