当对静态成员的调用在模板中是依赖的时候,C ++ 98标准在哪里指定? [英] Where does the C++98 standard specify when a call to a static member is dependent within a template?

查看:227
本文介绍了当对静态成员的调用在模板中是依赖的时候,C ++ 98标准在哪里指定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Clang 3.0 -std = c ++ 98编译,接受以下代码:

Compiling with Clang 3.0 -std=c++98, the following code is accepted:

template<int>
struct I
{
    typedef int Type;
};

template<class>
struct S
{
    static int f(int);
    //static int f(int*);

    // implicitly instantiates I<sizeof(int)>
    typedef I<sizeof(f(0))>::Type Type; 
};

S<int>::Type s;

取消注释'f'的重载会导致Clang报告错误missingtypename类型名称。 G ++ 4.8报告相同的错误,有或没有重载。 msvc10在有或没有过载的情况下不会出现任何错误。

Uncommenting the overload of 'f' causes Clang to report an error "missing 'typename' prior to dependent type name". G++ 4.8 reports the same error with or without the overload. msvc10 does not give any errors with or without the overload.

标准说明'f'是否依赖,'typename'是否需要?如果不需要'typename',标准在那里说明是否应该在这种情况下执行重载分辨率?

Where does the standard say whether or not 'f' is dependent and 'typename' is required? If 'typename' is not required, where does the standard say whether or not overload resolution should be performed in this scenario?

编辑:

澄清:我提到重载分辨率的原因是可能需要执行重载分辨率来确定常量表达式'sizeof(f(0))'的值。如果(当我假定)在确定表达式是否是类型相关时不执行重载分辨率,当依赖过载时,常量表达式sizeof(f(0))的值不可能(在解析时) of'f'exists:例如

To clarify: the reason I mention overload resolution is that it may be necessary to perform overload resolution to determine the value of the constant-expression 'sizeof(f(0))'. If (as I assume) overload resolution is not performed when determining whether an expression is type-dependent, the value of the constant-expression 'sizeof(f(0))' is impossible to determine (at parse time) when a dependent overload of 'f' exists: e.g.

template<int>
struct I
{
    typedef int Type;
};

template<class T>
struct S
{
    static T f(int);

    typedef typename I<sizeof(f(0))>::Type Type; 
};

S<int>::Type t;

使用Clang 3.0编译-std = c ++ 98,这不会产生错误。这似乎对我来说是正确的,因为标准认为一个表达式是类型依赖的,如果它是一个id表达式命名一个对象声明与依赖类型。

Compiling with Clang 3.0 -std=c++98, this produces no errors. This seems correct to me, because the standard deems an expression to be type-dependent if it is an id-expression naming an object declared with a dependent type.

推荐答案

涵盖此情况的C ++ 98标准的段落在[temp.dep.expr]中找到

The paragraph of the C++98 standard that covers this scenario is found in [temp.dep.expr]


如果id-expression包含:

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


  • 以依赖类型声明的标识符
  • >

这个措辞对于重载的标识符有些模糊,并且可能声明为多个类型, DR 541: http://www.open-std.org/ jtc1 / sc22 / wg21 / docs / cwg_defects.html

This wording is somewhat vague about identifiers that are overloaded and potentially declared with more than one type, as reported in DR 541 : http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html

通过将段落更改为:


如果id-expression包含:

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,

当考虑下面的代码时:

template<class T>
struct S
{
    static int f(int);
    static int f(int*);

    static T g(int*);
    static int g(int);

    static const int x = sizeof(f(0));
    static const int y = sizeof(g(0));
};

我的解释是标识符 f 表达式 f(0)不依赖,而表达式 g中的标识符 g (0)是依赖的。

My interpretation is that the identifier f in the expression f(0) is not dependent, while the identifier g in the expression g(0) is dependent.

当确定函数调用表达式是否依赖时 - 虽然不执行重载分辨率 - 功能。

When determining if a function-call expression is dependent - though overload resolution is not performed - all overloads of the function are considered.

这篇关于当对静态成员的调用在模板中是依赖的时候,C ++ 98标准在哪里指定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆