所以我最近遇到了MSVC一個問題,即它不會編譯我的代碼,根據MSVC開發者社區這一行是罪魁禍首(在頭引起編譯錯誤一樣<cmath>
和<type_traits>
差異
#define type typename //because type T looks infinitely better than typename T
的(應該)修正爲:
#define type typename1
問題是我無法找到「typename1」,什麼是它們之間的區別,爲什麼typename1不會導致數百錯誤,犯錯的任何文件當typename呢?爲typename定義別名只是一種不好的做法,我無法想象它會如何,但也許呢?
因此,要回到問題,typename和typename1如何不同,爲什麼此代碼打破?
#define type typename //this is supposedly the problem
#include <cstdint>
#include <cstdio>
#include <cmath> //this breaks
#include <type_traits> //this breaks
//as would any other header that includes corecrt_math or similar
//to reproduce the problem we need not include templates,
//but there would naturally be several since I want to use the 'type'
//identifier
int main()
{
return 0;
}
'typename'是關鍵字。讓它出現在不應該會導致錯誤的地方。這是宏應該避免的原因的一部分。 – NathanOliver
[typename](http://en.cppreference.com/w/cpp/keyword/typename)是C++中的保留關鍵字。 _typename1_不是。將'const'或'constexpr'選爲'define'。 – Ron
在'C++'中,你可能不想使用#define。 – drescherjm