void foo(){} int main-6ren">
gpt4 book ai didi

c++ - 已删除 "general"案例的专用模板函数无法使用 g++ <=4.8.0 和 clang++ 编译

转载 作者:IT老高 更新时间:2023-10-28 22:14:54 25 4
gpt4 key购买 nike

用老版本的g++(4.8.0,MinGW)编译项目,发现这段代码编译失败:

template<typename T>
void foo() = delete;

template<>
void foo<int>(){}

int main() {
foo<int>();
return 0;
}

如果发现基本情况已被删除,g++ 似乎甚至不会尝试寻找显式的特化。

mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ -std=c++11 buggy_deleted_template.cpp 
buggy_deleted_template.cpp: In function 'int main()':
buggy_deleted_template.cpp:8:14: error: use of deleted function 'void foo() [with T = int]'
foo<int>();
^
buggy_deleted_template.cpp:5:6: error: declared here
void foo<int>(){}
^
mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ --version
i686-w64-mingw32-g++ (rubenvb-4.8.0) 4.8.0
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

相反,g++ 4.8.4 和 5.2(在 Linux 上)不会提示。这是旧版本编译器的错误还是标准中的灰色区域?


附录

clang 3.4.1 似乎也不喜欢它:

mitalia@mitalia:~/scratch$ clang++ -std=c++11 buggy_deleted_template.cpp                                                             
buggy_deleted_template.cpp:5:6: error: redefinition of 'foo'
void foo<int>(){}
^
buggy_deleted_template.cpp:5:6: note: previous definition is here
buggy_deleted_template.cpp:8:5: error: no matching function for call to 'foo'
foo<int>();
^~~~~~~~
buggy_deleted_template.cpp:2:6: note: candidate template ignored: substitution failure [with T = int]
void foo() = delete;
^
2 errors generated.
mitalia@mitalia:~/scratch$ clang++ --version
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix

(并且评论中的 @Baum mit Augen 报告说它在 3.7 中仍然不起作用)

最佳答案

我不知道以下是否会有所启发,但我找到了defect report 941: Explicit specialization of deleted function template 状态为 C++11 的状态如下 (Emphasis Mine):

According to 14.7.3 [temp.expl.spec] paragraph 1, only non-deleted function templates may be explicitly specialized. There doesn't appear to be a compelling need for this restriction, however, and it could be useful to forbid use of implicitly-instantiated specializations while still allowing use of explicitly-specialized versions.

Proposed resolution (February, 2010):

Change 14.7.3 [temp.expl.spec] paragraph 1 as follows:

An explicit specialization of any of the following:

non-deleted function template

class template

non-deleted member function of a class template

static data member of a class template

member class of a class template

member class template of a class or class template

non-deleted member function template of a class or class template

can be declared...

标准草案的当前状态N452714.7.3 显式特化 [temp.expl.spec]:

1 An explicit specialization of any of the following:

(1.1) — function template

(1.2) — class template

(1.3) — variable template

(1.4) — member function of a class template

(1.5) — static data member of a class template

(1.6) — member class of a class template

(1.7) — member enumeration of a class template

(1.8) — member class template of a class or class template

(1.9) — member function template of a class or class template

...

所以我猜:

template<typename T>
void foo() = delete;

template<>
void foo<int>(){}

int main() {
foo<int>();
return 0;
}

是 C++11 标准兼容代码,应该被接受。

关于c++ - 已删除 "general"案例的专用模板函数无法使用 g++ <=4.8.0 和 clang++ 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33256589/

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