2017-05-19 33 views
1

我想在我的程序(類型和轉換函數)中添加一些關於參數的信息。參數的名稱是const。我寫了一些代碼,但在2017年MSVC它不能編譯(海合會或鏘是OK)MSVC錯誤C2971 for const wchar_t *作爲模板參數

#include <iostream> 
#include <string> 

namespace params 
{ 
    constexpr wchar_t CLIENT_LOGIN[] = L"ClientLogin"; 

    template<const wchar_t * ParamName> 
    struct convert_traits 
    { 
    }; 

    template<> 
    struct convert_traits<CLIENT_LOGIN> 
    { 
     using ConvertType = unsigned long long; 
     static ConvertType convert(const std::wstring &str) 
     { 
      return std::stoull(str); 
     } 
    }; 
} 

int main() { 

    std::cout << params::convert_traits<params::CLIENT_LOGIN>::convert(L"123.0") << std::endl; 

    return 0; 
} 

我收到錯誤:

main.cpp(15): error C2971: 'params::convert_traits': template parameter 'ParamName': 'params::CLIENT_LOGIN': a variable with non-static storage duration cannot be used as a non-type argument 
main.cpp(10): note: see declaration of 'params::convert_traits' 
main.cpp(6): note: see declaration of 'params::CLIENT_LOGIN' 
main.cpp(26): error C2971: 'params::convert_traits': template parameter 'ParamName': 'params::CLIENT_LOGIN': a variable with non-static storage duration cannot be used as a non-type argument 
main.cpp(10): note: see declaration of 'params::convert_traits' 
main.cpp(6): note: see declaration of 'params::CLIENT_LOGIN' 

,不能發現MSVC修復。

+0

該變量位於命名空間範圍 - 因此具有靜態存儲持續時間。 –

+0

我希望這是'template '和'template '之間的區別,但是[@Rook](http://stackoverflow.com/users/1450890/rook )說後者也失敗了:-( –

回答

0

解決了

extern const wchar_t CLIENT_LOGIN[] = L"ClientLogin"; 

但如果在頭定義它不能正常工作。