gpt4 book ai didi

c++ - 是否对参与偏序的类型执行实例化

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

最近发现GCC改变了偏序时的行为,具体情况如下:

#include <iostream>
template<class T>
struct unknow_context{
using type = int;
};
template<class U>
void show(typename unknow_context<U>::type, U){ // candidate #1
std::cout<<"#1\n";
}

template<class T>
void show(int, T){ // candidate #2
std::cout<<"#2\n";
}
int main(){
show(0,0);
}
结果是, Clang打印 #2 ( Clang 的任何版本都打印出一致的结果)。但是, GCC有不同的行为。 GCC 的旧版本打印 #2 ,而是最新的 GCC提示候选函数不明确。让我们看看标准对部分排序的看法。
temp.deduct.partial#2

The deduction process uses the transformed type as the argument template and the original type of the other template as the parameter template. This process is done twice for each type involved in the partial ordering comparison: once using the transformed template-1 as the argument template and template-2 as the parameter template and again using the transformed template-2 as the argument template and template-1 as the parameter template.


因此,我们可以得到候选 #1 的两组 P/A 对。和 #2 , 分别。一种是改造后的 #1作为A,原 #2作为P。另一个是原文 #1作为 P,转换后的 #2作为A。所以这两组将给出如下:
#a  
|--------|------------------------------------------|
| P (#2) | A (#1) |
|--------|------------------------------------------|
| int | typename unknow_context<UniqueA>::type |
|--------|------------------------------------------|
| T | UniqueB |
|--------|------------------------------------------|
T可以推导出 UniqueB .对于第一组,规则说:

If a particular P contains no template-parameters that participate in template argument deduction, that P is not used to determine the ordering.


所以, 我们先把它们放在一边,稍后再考虑 .
#b  
|----------------------------------|-------|
|P (#1) |A (#2) |
|----------------------------------|-------|
| typename unknow_context<U>::type |int |
|----------------------------------|-------|
| U |UniqueA|
|----------------------------------|-------|
对于非演绎的上下文,它的值可以从别处获得。

In certain contexts, however, the value does not participate in type deduction, but instead uses the values of template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.


所以,我们可以忽略这对 typename unknow_context<U>::type/ int , 并考虑 U/ UniqueA . U可以推导出 UniqueA .

If deduction succeeds for a given type, the type from the argument template is considered to be at least as specialized as the type from the parameter template.


来自 #b ,我们得到 #2的结果至少与 #1 一样专业.
问题在 #a set .第二对没有问题,可以说 #1的第二部分至少与 #2 的第二部分一样专业.
但是,函数模板 F 是否比函数模板 G 更专业的规则定义为:

Function template F is at least as specialized as function template G if, for each pair of types used to determine the ordering, the type from F is at least as specialized as the type from G. F is more specialized than G if F is at least as specialized as G and G is not at least as specialized as F.


所以,我们注意对 int / typename unknow_context<UniqueA>::type ,尽管规则说“P 不用于确定排序”。但是,标准中的重要说明说:

[ Note: Under [temp.deduct.call] and [temp.deduct.partial], if P contains no template-parameters that appear in deduced contexts, no deduction is done, so P and A need not have the same form.  — end note ]


所以,就目前而言,从 P/A set #a , #1仍然至少与 #2 一样专业(扣分成功)。所以,我认为最新的 GCC应该是正确的(模棱两可,两者都不比另一个更专业)。
问题一:
哪个编译器是正确的?
问题2:
是否在部分排序期间执行特化的实例化?该标准似乎没有指定是否将执行实例化。
#include <iostream>
template<class T>
struct unknow_context{
using type = T;
};
template<class U>
void show(typename unknow_context<U>::type, U){
std::cout<<"#1\n";
}

template<class T>
void show(T, T){
std::cout<<"#2\n";
}
int main(){
show(0,0);
}
全部选择 #2 .
我担心特定的 P/A pair其中,即:
|----|------------------------------------------------------------|
|P |A |
|----|------------------------------------------------------------|
|T |typename unknow_context<UniqueA>::type /*Is it equivalent to|
| | UniqueA? */ |
|----|------------------------------------------------------------|
|T |UniqueA |
|----|------------------------------------------------------------|
是否 typename unknow_context<UniqueA>::type将计算为 UniqueA ?似乎所有编译器都对待 typename unknow_context<UniqueA>::type作为唯一类型而不是计算到 UniqueA .

最佳答案

(由于这个答案与 OP 一致并且不同意 Clang 和 GCC(主干)的实现,它可能是一个有点不完整的答案,但至少它突出了部分排序规则的一些现有问题,特别是对于部分排序涉及非推断上下文))

Question 1: which compiler is correct?


让我们首先注意,从 GCC 11(/trunk) 开始,两个编译器都同意他们的解释,并选择候选 #2 比候选 #1 更专业,并且根据 [over.match.best]/1.7重载决议选择前者作为最佳可行函数。
但是,您的论点 [temp.func.order]似乎是有效的,尤其是对 [temp.deduct.partial]/4 的强调:

[...] If a particular P contains no template-parameters that participate in template argument deduction, that P is not used to determine the ordering.


意思是

[...] for each pair of types used to determine the ordering [...]


[temp.deduct.partial]/10不应该考虑 (P, A)(int, typename unknow_context<UniqueA>::type)对于排序,对于剩余的对,候选#1 至少与候选#2 一样专业,这意味着候选#1 至少是根据 [temp.deduct.partial]/10 指定为候选#2。
因此,我认为 Clang 是错误的,并且根据 GCC 11(/trunk),GCC 再次是错误的,但正如我在下面强调的那样,在涉及非推断上下文的边缘情况下,部分排序规则在历史上一直是,未指定( 02-0051/N1393 解决了其中的许多问题),而如今,至少是模糊的(可能仍然未指定),因为我们看到它们的实现存在很多差异。

Question 2: whether the instantiation of specialization be performed during partial ordering?


我不确定最相关的部分;它可能属于 [temp.inst]/9 ,

An implementation shall not implicitly instantiate [...], unless such instantiation is required.


[temp.deduct]/8 的非规范性注释:

[ Note: The substitution into types and expressions can result in effects such as the instantiation of class template specializations and/or function template specializations, [...] end note ]


但是,是的, unknown_context 的特化的合理实例化作为部分排序的一部分,作为推导参数的模板参数替换的一部分是必需的。如果编译器的情况如此,并且 GCC 和 Clang 都同意,我们可以使用注入(inject)的 friend 技巧来强制可诊断的 ODR 违规,并拒绝以下程序:
// Due to the injected friend, the identity class may 
// only be instantiated once within a given TU, or
// the program, diagnosable ([basic.odr.def]/1).
template<class T>
struct identity {
using type = T;
friend void f() {}
};
void f();

template<class U>
void show(typename identity<U>::type, U) {}

template<class T>
void show(T, T) {}

int main(){
identity<char> i; // f() now defined
f(); // OK
show(0,0); // error: redefines f() as part of
// substitution in partial ordering.
}
带有指导性错误:
error: redefinition of 'f'
friend void f() {}
^
note: in instantiation of template class
'identity<int>' requested here
void show(typename identity<U>::type, U) {}

note: while substituting deduced template arguments
into function template 'show' [with U = int]


部分排序和非推导上下文中的历史不确定性
我们可以从活跃/开放 CWG issue 455开始:

455. Partial ordering and non-deduced arguments

It's not clear how overloading and partial ordering handle non-deduced pairs of corresponding arguments.

[...]

John Spicer: There may (or may not) be an issue concerning whether nondeduced contexts are handled properly in the partial ordering rules


并注意编译器,尤其是 Clang 和 GCC,在如何在涉及非推导上下文的边缘情况下应用部分排序规则方面始终存在分歧。
来自 GCC 的 Jason Merrill 写道 CWG issue 1337 ,它被标记为 CWG 问题 455 的拷贝。Jason 积极参与许多开放的 GCC 错误报告,特别注意到
  • Bug 86193 - 具有依赖类型的非类型模板参数的部分排序

  • 那 [ 重点矿]

    Jason Merrill 2018-06-18 19:09:13 UTC

    which, similarly, G++ (and EDG) rejects, and clang accepts. I think G++ is right here: [...]

    This does seem like an underspecified area in the standard.


    以及
  • Bug 91133 - [8/9/10/11 回归] 错误的“部分特化不比特化”错误


  • This is really a question of partial ordering; [...]

    This was rejected as ambiguous by GCC going back at least to 4.1. It is also rejected by EDG/icc. It is accepted by clang and msvc, like the original testcase.

    The issue is with the partial ordering deduction of #1 from #2: we deduce int for U from the second argument, and Id::type for U from the third argument, and those don't agree, so deduction for the third argument fails in both directions, and the functions are ambiguous.

    This is related to open core issues 455 and 1337.

    I don't know what rationale clang/msvc are using to conclude that #2 is more specialized.


    因此,根据上面的引述,他们对(可能未指定的)标准的历史不同解释似乎是有意的。这种实现差异通常是标准相关部分模糊的标志,充其量是(未指定,最坏的情况)。
    另见 GCC bug report 67228 .

    来自 GCC 的歧义错误只是回归吗?

    The older version of GCC prints #2, instead, the latest GCC complains the candidate functions are ambiguous.


    如上所述,以下程序的 GCC:s 行为
    #include <iostream>
    template<class T>
    struct unknow_context{
    using type = int;
    };
    template<class U>
    void show(typename unknow_context<U>::type, U){ // candidate #1
    std::cout<<"#1\n";
    }

    template<class T>
    void show(int, T){ // candidate #2
    std::cout<<"#2\n";
    }
    int main(){
    show(0,0);
    }

    如下:
  • 直到并包括 GCC 7.3.0:打印 #2 (同clang)
  • GCC 8/9/10:错误:不明确的调用
  • GCC 主干/11:返回打印 #2

  • 我还没有找到关于 [8/9/10] 回归的错误报告,但似乎 GCC 现在重新使用与 Clang 相同的解释(接受程序并发现 #2 更专业),这两者我们中的一些人似乎同意这是错误的(候选人#1 应该被认为至少与候选人#2 一样专业)。

    关于c++ - 是否对参与偏序的类型执行实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65210223/

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