gpt4 book ai didi

c++ - 非类型模板参数中的占位符类型是否可以涉及作为模板参数传递的函数的重载解析?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:39 24 4
gpt4 key购买 nike

this question 的后续行动.假设占位符可用于推断构成非类型模板参数的函数指针的结果类型。 c++17 是否允许对传递给模板函数的名称执行重载解析——在不知道结果类型的情况下执行隐式转换需要这样做?

template <auto(*)(int)>
struct Foo { };

int bar(int);
float bar(float);

int main() {
static_cast<void>(Foo<bar>{});
}

[gcc]以及[clang]似乎接受代码。

最佳答案

是的,根据very bullet Rakete1111 pointed out是允许的.而且没有必要只是假设它可以完成,它是根据占位符类型推导规则完成的 [dcl.type.auto.deduct]/4 ,强调我的:

If the placeholder is the auto type-specifier, the deduced type T' replacing T is determined using the rules for template argument deduction. Obtain P from T by replacing the occurrences of auto with either a new invented type template parameter U or, if the initialization is copy-list-initialization, with std​::​initializer_­list. Deduce a value for U using the rules of template argument deduction from a function call, where P is a function template parameter type and the corresponding argument is e. If the deduction fails, the declaration is ill-formed. Otherwise, T' is obtained by substituting the deduced U into P.

在哪里[temp.deduct.call]/6有这一段,与您的用例有关:

When P is a function type, function pointer type, or pointer to member function type:

  • If the argument is an overload set containing one or more function templates, the parameter is treated as a non-deduced context.

  • If the argument is an overload set (not containing function templates), trial argument deduction is attempted using each of the members of the set. If deduction succeeds for only one of the overload set members, that member is used as the argument value for the deduction. If deduction succeeds for more than one member of the overload set the parameter is treated as a non-deduced context.

这就是它的全部荣耀。

关于c++ - 非类型模板参数中的占位符类型是否可以涉及作为模板参数传递的函数的重载解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48485446/

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