ADL在constexpr函数中不起作用(仅适用于clang) [英] ADL does not work in constexpr functions (clang only)

查看:115
本文介绍了ADL在constexpr函数中不起作用(仅适用于clang)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用MSVC和gcc进行编译,但不能使用clang进行编译。为什么会这样?

The following code compiles with MSVC and gcc, but not with clang. Why is that so?

如果 CallFoo()为<$ c $,则ADL似乎无法工作c> constexpr 。

template <class T>
constexpr void CallFoo  ()          // Remove constexpr to fix clang compilation error.
{
    Foo (T ());
}


class Apple {};


int main ()
{
    CallFoo<Apple> ();
}


constexpr void Foo (Apple)
{
}

C语错误消息(在godbolt.org上查看):

Clang error message (see on godbolt.org):

<source>:4:5: error: use of undeclared identifier 'Foo'
    Foo (T ());
    ^
<source>:13:5: note: in instantiation of function template specialization 'CallFoo<Apple>' requested here
    CallFoo<Apple> ();
    ^


推荐答案

声明应显示在实例化点,因此clang有权拒绝您的代码。重新排序功能可修复编译问题:

Declaration should be visible at the point of instantiation, so clang has right to reject your code. Reordering functions fixes compilation:

constexpr void Foo (Apple)
{
}

int main ()
{
    CallFoo<Apple> ();
}

演示

事实是文件末尾也是实例化点,并且gcc / MSVC仅应考虑这一点:/

Fact is that end of file is a point of instantiation too, and gcc/MSVC should only consider this one :/

这篇关于ADL在constexpr函数中不起作用(仅适用于clang)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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