下面的網(wǎng)址是最有名的兩個(gè)在線C++編譯網(wǎng)站:
http://www.dinkumware.com/exam/default.aspx
http://www.comeaucomputing.com/tryitout/
許多在線判題系統(tǒng)也可以用來(lái)進(jìn)行在線編譯:
http://acm.zju.edu.cn/
http://acm.pku.edu.cn/JudgeOnline/
http://acm.uva.es/
你可能會(huì)好奇這些網(wǎng)站使用的編譯器的版本和編譯參數(shù)到底是什么。有些網(wǎng)站會(huì)提供這些信息,而有些則不會(huì)。那么你就只能靠自己去發(fā)現(xiàn)這些“秘密”了。
工欲善其事,必先利其器。讓我們先看看一些從編譯器獲取信息的小技巧。
1. 輸出宏
在C++中, 字符串常數(shù)不能作為模板參數(shù)。大多數(shù)編譯器會(huì)在錯(cuò)誤信息中同時(shí)輸出字符串的內(nèi)容。下面的代碼展示了這一技巧。
template<const char *> class A {};
#define F0(x) #x
#define F1(x) F0(x)
#define V1 F1(__GNUC__)
int main()
{
A<V1> a1;
}
這里,宏F0和F1用于將整型轉(zhuǎn)換成字符串。編譯器會(huì)輸出類似于"literal "3" is not a valid template argument because it is the address of an object with static linkage"的出錯(cuò)信息。里面的"3"就是你想知道的宏的內(nèi)容。
2. 輸出常數(shù)
同樣,浮點(diǎn)數(shù)是不能作為模板參數(shù)的。編譯器通常會(huì)在錯(cuò)誤信息中包含浮點(diǎn)數(shù)的值。利用這一點(diǎn),我們可以輸出浮點(diǎn)常數(shù)。不過(guò)很不幸的是,VC會(huì)隱式的將浮點(diǎn)類型的模板參數(shù)轉(zhuǎn)換成int(參見(jiàn)https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296008)
下面是一種輸出整型常量的方法:
template<int N>
class A
{
private:
A();
};
int main()
{
A<sizeof(char)> a;
}
3. 輸出變量類型
有時(shí)候,你可能需要知道某些變量的實(shí)際類型,這時(shí)候你可以使用下面的代碼:
struct Dummy {};
void Fun(const Dummy &);
int main()
{
Fun(1+1U);
}
PS:如果你使用的是gcc,那么它會(huì)輸出"not convert `2' to `const Dummy&'",所以你需要將Fun的聲明改成"template<typename T> void Fun(T);"(換句話說(shuō),在gcc中上面的代碼也可以用于輸出常數(shù)的值)
4. 輸出包含路徑
如果要獲取STL頭文件的路徑,你可以使用:
#include <ext/hash_set>
using __gnu_cxx::hash_set;
int main()
{
hash_set<> m;
}
PS:這里也可以使用vector。
好,現(xiàn)在是時(shí)候牛刀小試了。關(guān)于如何獲取編譯器的版本信息,可以參考這篇文章:Pre-defined Compiler Macros
下面是利用上面介紹的技巧獲得的dinkumware網(wǎng)站的一些資料:
1. VC
版本 (_MSC_FULL_VER):
VC8 140050727
VC7.1 13103077
VC7 13009466
VC6 12008804
包含路徑:
D:\cplt501_vc_source\include (with _CPPLIB_VER=501)
2. EDG
版本(__EDG_VERSION__):
3.05
編譯參數(shù):
edgcc --alt -D_WIN32 -D_M_IX86 --no_inlining --diag_suppress=1290 --long_long --wchar_t_keyword -D_C99 -ID:\cplt501_gen_source/include/c -ID:\cplt501_gen_source/include -IC:\PROGRA~1\MICROS~2.NET\VC7/include --exceptions --microsoft -c sourceFile.cpp
因?yàn)槭褂昧薞C兼容模式進(jìn)行編譯,所以編譯器可能會(huì)模擬VC的部分bug
3. GCC
版本:
3.2.0
包含路徑:
D:/cplt501_gen_source/include and D:/Mingw/include/c++/3.2/
可以看到這里使用的GCC的版本已經(jīng)相當(dāng)陳舊了
文章出處:http://www.diybl.com/course/3_program/c++/cppsl/2008108/149011.html