Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe a bug of concept and vector #124467

Open
scarsty opened this issue Jan 26, 2025 · 3 comments
Open

Maybe a bug of concept and vector #124467

scarsty opened this issue Jan 26, 2025 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue

Comments

@scarsty
Copy link

scarsty commented Jan 26, 2025

These codes cannot be complied with clang 19, but of no problem with MSVC and GCC.

#include <iostream>
#include <string>
#include <vector>

class A
{
    std::string a;
    std::vector<A> v;

public:
    A(int i) { a = std::to_string(i); }

    int toInt() const { return atoi(a.c_str()); }

    void pushBack(const A& a) { v.push_back(a); }    // line 1

    template <typename T>
        requires requires(const A& a, T& t) { A2T(a, t); }    // line 2
    operator T() const
    {
        T t;
        A2T(*this, t);
        return t;
    }
};

inline void A2T(const A& a, int& f) { f = a.toInt(); }

int main()
{
    A a(20);
    int f = a;
    std::cout << f << std::endl;
    return 0;
}

The error is

<source>:43:9: error: no viable conversion from 'A' to 'int'
   43 |     int f = a;
      |         ^   ~
<source>:23:5: note: candidate template ignored: constraints not satisfied [with T = int]
   23 |     operator T() const
      |     ^
<source>:22:47: note: because 'A2T(a, t)' would be invalid: use of undeclared identifier 'A2T'
   22 |         requires requires(const A& a, T& t) { A2T(a, t); }
      |                                               ^
1 error generated.

But if one of the line 1 or 2 is removed, things become OK. I guess it might be a bug of concept, does anyone have other idea?

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue and removed new issue labels Jan 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 26, 2025

@llvm/issue-subscribers-clang-frontend

Author: SunTY (scarsty)

These codes cannot be complied with clang 19, but of no problem with MSVC and GCC.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

class A
{
    std::string a;
    std::vector&lt;A&gt; v;

public:
    A(int i) { a = std::to_string(i); }

    int toInt() const { return atoi(a.c_str()); }

    void pushBack(const A&amp; a) { v.push_back(a); }    // line 1

    template &lt;typename T&gt;
        requires requires(const A&amp; a, T&amp; t) { A2T(a, t); }    // line 2
    operator T() const
    {
        T t;
        A2T(*this, t);
        return t;
    }
};

inline void A2T(const A&amp; a, int&amp; f) { f = a.toInt(); }

int main()
{
    A a(20);
    int f = a;
    std::cout &lt;&lt; f &lt;&lt; std::endl;
    return 0;
}

The error is

&lt;source&gt;:43:9: error: no viable conversion from 'A' to 'int'
   43 |     int f = a;
      |         ^   ~
&lt;source&gt;:23:5: note: candidate template ignored: constraints not satisfied [with T = int]
   23 |     operator T() const
      |     ^
&lt;source&gt;:22:47: note: because 'A2T(a, t)' would be invalid: use of undeclared identifier 'A2T'
   22 |         requires requires(const A&amp; a, T&amp; t) { A2T(a, t); }
      |                                               ^
1 error generated.

But if one of the line 1 or 2 is removed, things become OK. I guess it might be a bug of concept, does anyone have other idea?

@zyn0217 zyn0217 added the concepts C++20 concepts label Jan 27, 2025
@shafik
Copy link
Collaborator

shafik commented Jan 27, 2025

edg agrees w/ clang: https://godbolt.org/z/9oM5sdfj8

Using --stdlib=libc++ does not change the results.

@cor3ntin
Copy link
Contributor

It looks like lookup in requires expression doesn't happen in the correct context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue
Projects
None yet
Development

No branches or pull requests

6 participants