gpt4 book ai didi

c++ - const指针专门化可使用MSVC生成未解析的符号,并且适用于GCC/Clang

转载 作者:行者123 更新时间:2023-12-02 10:03:24 26 4
gpt4 key购买 nike

请看下面的代码:

// header.h
#pragma once

template <typename T>
void test(T*);

// lib.cpp
#include "header.h"

template <>
void test(int* const)
{
}

// main.cpp
#include "header.h"

int main() {
int* ptr = nullptr;
test(ptr);
}

当MSVC(2015,2019)产生未解析的外部符号“void __cdecl test(int *)”时,gcc和clang都可以毫无问题地进行编译和链接。

请注意,如果从lib.cpp中删除了const关键字,则MSVC可以很好地链接代码。

谁在这里?这是MSVC的错误吗?

最佳答案

该程序格式错误,无需诊断。

[temp.expl.spec]/6 If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.

关于c++ - const指针专门化可使用MSVC生成未解析的符号,并且适用于GCC/Clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432146/

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