ADL does not work in constexpr functions (clang only)

我与影子孤独终老i 提交于 2019-12-23 09:20:03

问题


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

It seems like if ADL would not work if CallFoo () is constexpr. See the comment.

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)
{
}

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> ();
    ^

回答1:


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> ();
}

Demo

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



来源:https://stackoverflow.com/questions/57501997/adl-does-not-work-in-constexpr-functions-clang-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!