gpt4 book ai didi

c++ - VC++ 2015错误: “an expression involving objects with internal linkage cannot be used as a non-type argument”

转载 作者:行者123 更新时间:2023-12-03 07:15:26 25 4
gpt4 key购买 nike

以下sample code确实在VC++ 2019,clang++和g++中进行编译,但在VC++ 2015中未进行编译。

namespace ns
{
template<const char* str>
struct Foo
{
};

static const char name[] = "Test";
Foo<name> foo;
}

int main()
{
}
VC++ 2015有任何解决方法吗?我假设代码符合要求,但VC++ 2015的一个错误已在VC++ 2019中修复。我迁移到VC++ 2019,但我的公司建立在VC++ 2015中。

最佳答案

MSVC 2015并不完全支持C++ 11,带有内部链接的非类型模板参数是C++ 11功能的一个示例,VC++直到14.14版(VS 2017 15.8)才支持。
我可以想到3个解决方案:

  • 不要通过从static const删除char name[]说明符来将模板参数与内部链接一起使用
  • 将编译器升级到VC++ 14.20+(MSVC 2019)
  • 使用功能检测和/或条件编译

  • 关于条件编译,可以这样实现:
    #if !defined(_MSC_VER) || _MSC_VER > 1914 // this part can be in a global config.h
    # define INTERNAL static const
    #else
    # define INTERNAL
    #endif

    namespace ns
    {
    template<const char* str>
    struct Foo
    {};

    INTERNAL char name[] = "Test";
    Foo<name> foo;
    }

    int main()
    {
    }

    关于c++ - VC++ 2015错误: “an expression involving objects with internal linkage cannot be used as a non-type argument”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64644209/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com