cplusplus / CWG

Core Working Group
23 stars 7 forks source link

CWG2905 [temp.dep.constexpr] value-dependence of `noexcept` `(` *expression* `)` #554

Open MitalAshok opened 2 weeks ago

MitalAshok commented 2 weeks ago

Full name of submitter: Mital Ashok

Reference: [temp.dep.constexpr]

Issue description: Whether the operand of a noexcept operator is type-dependent is not enough to know if the noexcept operator is value-dependent. There are plenty of non-type-dependent expressions where whether or not it is potentially-throwing is dependent.

For example:

template<typename T>
void f() {
    noexcept(new int(T{}));
    noexcept(static_cast<int>(T{}));
    noexcept(typeid(*T{}));
    noexcept(delete T{});
}

GCC already treats all of these as value-dependent. Clang treats noexcept(new int(T{})) as a not value-dependent false but the rest of them as value-dependent.

Suggested resolution:

Remove noexcept from [temp.dep.constexpr]p2 so it becomes value-dependent if its operand is value-dependent according to [temp.dep.constexpr]p1

Expressions of the following form are value-dependent if the unary-expression or expression is type-dependent or the type-id is dependent:
sizeof unary-expression [...] noexcept ( expression )

jensmaurer commented 2 weeks ago

CWG2905

MitalAshok commented 2 weeks ago

Actually this seems this is at odds with CWG1074.

Also value-dependence is only for constant expressions, which noexcept doesn't require (esp. since throwing an exception is not a constant expression)

Also some cases are missing from value-dependence still,

struct X {
    virtual void f();
};

template<typename T>
void g() {
    X value;
    noexcept(dynamic_cast<T&>(value));
}

(I think these are all caught by CWG2066 / CWG2109)

Perhaps noexcept should not rely on value-dependence at all and be:

noexcept ( expression ) is value-dependent if expression is an expression involving a template parameter

(See also: CWG1172, making this easier if "instantiation-dependent" was defined)

jensmaurer commented 2 weeks ago

Yeah. Updated.