從 SVN (https://protobuf.googlecode.com/svn/trunk) 下載 Protobuf 源碼,打開 vsprojects 目錄下的 VC2008 工程並轉換為 VC2012。編譯 gtest_main 項目時爆出錯誤:
1>e:\protobuf\gtest\include\gtest\gtest-printers.h(556): error C2977: 'std::tuple' : too many template arguments
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(73) : see declaration of 'std::tuple'
1>e:\protobuf\gtest\include\gtest\gtest-printers.h(564): error C2977: 'std::tuple' : too many template arguments
<utility> 中 std::tuple 實現基於可變參數模板,但僅支持最多 10 個模板參數(C++0x 早期實現限制)std::tuple,導致編譯失敗從 googletest 官方倉庫 下載支持 VS2012+ 的版本(1.7.0 及以上),替換 Protobuf 源碼樹中的 gtest 目錄。
編輯 gtest-printers.h 第 556、564 行附近,將超大元組拆分為巢狀元組或改用結構體:
// 原代碼可能類似:
// std::tuple<T1, T2, ..., T11, T12> // 12 參數超限
// 修改為:
std::tuple<std::tuple<T1, T2, ..., T10>, T11, T12> // 巢狀,每層 ≤ 10 參數
// 或定義結構體
struct BigTuple { T1 a; T2 b; ... T12 l; };
在項目屬性 → C/C++ → 預處理器 → 預處理器定義中添加:
_VARIADIC_MAX=15
警告:這會修改標準庫內部行為,可能導致 STL 其他部分不穩定,僅作臨時測試用。
在解決方案中右鍵 gtest_main、gtest 等項目 → 卸載項目,或配置屬性 → 通用 → 配置類型 改為「實用程式」並禁用建置。