gpt4 book ai didi

c++ - 如果一个函数类型只依赖于它自己的模板参数,它是否依赖于它?

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

我发现当前 C++ 编译器 (clang/gcc) 确定名称是否依赖的方式不一致。在下面的示例中,A::f 是相关的,但 ::f 不是,导致使用后者时出错。

template<typename>
struct B
{
typedef int Type;
};

template<typename U>
static U f(U u);

template<typename T>
struct A
{
template<typename U>
static U f(U u);

typename B<decltype(f(0))>::Type m1; // typename required
B<decltype(::f(0))>::Type m2; // typename not required
};

不一致的部分是 A::f 的声明不依赖于 A 的模板参数,这意味着似乎没有必要将其视为依赖名称.

此行为似乎包含在 C++11 标准中的以下措辞中:

[temp.dep.expr]/3

An id-expression is type-dependent if it contains

  • an identifier associated by name lookup with one or more declarations declared with a dependent type

[temp.dep.type]/3

A type is dependent if it is

  • a compound type constructed from any dependent type

::f 的声明显然是不依赖的,因为它的类型只依赖于它自己的模板参数。为什么要区别对待 A::f

最佳答案

我认为基于标准,f实际上是非依赖性的。

14.6.2.2 Type-dependent expressions [temp.dep.expr]

3 An id-expression is type-dependent if it contains

  • an identifier associated by name lookup with one or more declarations declared with a dependent type,

这同样适用于全局模板函数和成员模板函数:一点也不。 U 的返回类型依赖于模板函数的定义,但对于调用者,f<int> 的函数类型已经从U(U)转化而来至 int(int) .无论如何,它无法解释为什么编译器会区别对待这两种情况,也无法解释为什么非模板成员函数也被视为依赖。

  • a template-id that is dependent,
  • a conversion-function-id that specifies a dependent type, or

这些不适用。没有 <>必须始终存在于 template-id 中,并且没有调用转换函数。

  • a nested-name-specifier or a qualified-id that names a member of an unknown specialization;

见下文。

or if it names a static data member of the current instantiation that has type "array of unknown bound of T" for some T (14.5.1.3).

这也不适用:不涉及数组。

所以这取决于f是未知特化的成员。但它不是:

14.6.2.1 Dependent types [temp.dep.type]

5 A name is a member of an unknown specialization if it is

  • A qualified-id in which [...].
  • A qualified-id in which [...].
  • An id-expression denoting the member in a class member access expression (5.2.5) in which [...].

这些不能申请:f既不是限定的也不是类成员访问表达式的一部分。

因为唯一的方法f可以依赖是它是否是未知特化的成员,并且不是未知特化的成员,f不得依赖。

至于为什么编译器仍然将其视为依赖项,我没有答案。我在这里回答的某些部分是错误的,编译器有错误,或者编译器遵循不同版本的 C++ 标准。使用一个无论名称是否依赖都有效的示例进行测试显示了编译器处理的一些变化:

#include <cstdio>

void f(const char *s, ...) { std::printf("%s: non-dependent\n", s); }

struct S1 { };

template <typename T>
struct S2 {
static S1 a;
static S1 b() { return {}; }
template <typename U>
static U c() { return {}; }
static void z() {
f("S1()", S1()); // sanity check: clearly non-dependent
f("T()", T()); // sanity check: clearly dependent
f("a", a); // compiler agreement: non-dependent
f("b()", b()); // compiler disagreement: dependent according to GCC 4.8, non-dependent according to clang
f("c<T>()", c<T>()); // sanity check: clearly dependent
f("c<S1>()", c<S1>()); // compiler agreement: dependent
f("decltype(b())()", decltype(b())()); // compiler agreement: dependent
}
};

void f(const char *s, S1) { std::printf("%s: dependent\n", s); }

// Just to show it's possible to specialize the members
// without specializing the full template.
template <>
S1 S2<S1>::b() { return {}; }
template <>
template <>
S1 S2<S1>::c<S1>() { return {}; }

int main() {
S2<S1>::z();
}

clang 处理 b() 的差异, decltype(b())() , 和 c<S1>()对我来说特别麻烦。这没有任何意义。他们显然都同样依赖。我可以从实现的角度理解,必须注意不要为成员函数生成代码,因为可能有 S2<S1>::b 的特化。或 S2<S1>::c<S1> ,但这适用于所有人,并且对返回类型没有影响。

关于c++ - 如果一个函数类型只依赖于它自己的模板参数,它是否依赖于它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663038/

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