C++模板至少檢查兩次。首先,定義一個模板&,其次是實例化。模板成功實例化後,它處於安全狀態。我的問題是,當模板成功定義爲&時,模板所在的州的名稱是什麼? 如果我將宏與模板進行比較,是否成功的宏「實例化」後代碼是類型安全的?C++模板只是「語法安全」,而不是先安全的?
#define BAR(x) return x;
^
// BAR is preprocessor-grammar-safe here?
struct Bar
{
static void bar() {}
};
^
// Bar is type safe at this point?
template<typename T>
void foo()
{
T::bar();
}
^
// foo is C++-grammar safe at this point?
int main()
{
foo<Bar>();
^
// foo is type safe at this point?
foo<int>();
^
// foo is ill-formed here?
BAR(0);
^
// BAR is type safe at this point?
return 0;
}
我不確定*語法安全*有一個確切的含義,我相信你的問題的答案可能是編譯器的具體.... –
@BasileStarynkevitch說編譯器開發人員:) – 2013-12-17 19:05:11
這是一個加載的問題。你說很多聽起來像是文字的東西,但實際上並沒有任何意義。因此,很難做出迴應。 –