const wchar_t *的MSVC错误C2971作为模板参数

时间:2017-05-19 11:40:11

标签: c++ templates

我想在程序中添加一些有关参数的信息(类型和转换函数)。参数名称是const。我为它编写了一些代码,但它不能在MSVC 2017中编译(在GCC或Clang上都可以)

CREATE TRIGGER AuditProcChanges
ON DATABASE
FOR CREATE_PROC, ALTER_PROC, DROP_PROC, CREATE_TABLE, ALTER_TABLE, DROP_TABLE
AS

DECLARE @ed XML
SET @ed = EVENTDATA()

INSERT INTO DDLAudit (PostTime, DatabaseName, Event, ObjectName, TSQL, Login) 
VALUES
(
    GetDate(),
    @ed.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
    @ed.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
    @ed.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
    @ed.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)'),
    @ed.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
) 

我收到错误:

#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;
}

并且无法找到MSVC的修复程序。

1 个答案:

答案 0 :(得分:0)

解决了

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

但如果在标题中定义它则无效。