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

[BUG] Template deduction fails #595

Open
mike919192 opened this issue Aug 17, 2023 · 2 comments · May be fixed by #533
Open

[BUG] Template deduction fails #595

mike919192 opened this issue Aug 17, 2023 · 2 comments · May be fixed by #533
Labels
bug Something isn't working

Comments

@mike919192
Copy link

Hi. I found an instance where template deduction works in cpp but does not in cpp2. Here is the minimal example:

#include <iostream>
#include <array>
#include <complex>

//Syntax1 here
constexpr size_t N {64};
constexpr uint n {7};

//Syntax2 here
main: () -> int = {
    
    expected: std::array<double, N> = 0;

    observed: std::array<double, N> = 0;
    observed.at(n) = 1.0;

    //template deduction should work here but doesnt
    max_error: double = calc_max_error(observed, expected);
    //max_error: double = calc_max_error<N>(observed, expected);

    std::cout << max_error << "\n";

}

calc_max_error: <N: size_t> (observed: std::array<double, N>, expected: std::array<double, N>) -> double =
{
    errors: std::array<double, N> = 0;

    i: size_t = 0;
    for errors next i++ do (inout error) {
        error = std::abs(observed.at(i) - expected.at(i));
    }

    return std::max_element(errors.begin(), errors.end())*;
}

Using gcc11. Here is the compiler output

Starting build...
"../cppfront/cppfront/source/cppfront" *.cpp2 && "/usr/bin/g++" -fdiagnostics-color=always -I../cppfront/cppfront/source/ -std=c++20 -g *.cpp -o /config/workspace/hellocpp2/min_example
min_example.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)

min_example.cpp2: In function ‘int main()’:
min_example.cpp2:18:37: error: no matching function for call to ‘calc_max_error(std::array<double, 64>&, std::array<double, 64>&)’
   18 |     max_error: double = calc_max_error(observed, expected);
      |                       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
min_example.cpp2:25:39: note: candidate: ‘template<long unsigned int N> double calc_max_error(cpp2::in<std::array<double, N> >, cpp2::in<std::array<double, N> >)’
   25 | calc_max_error: <N: size_t> (observed: std::array<double, N>, expected: std::array<double, N>) -> double =
      |                                       ^~~~~~~~~~~~~~
min_example.cpp2:25:39: note:   template argument deduction/substitution failed:
min_example.cpp2:18:37: note:   couldn’t deduce template parameter ‘N’
   18 |     max_error: double = calc_max_error(observed, expected);
      |                       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

When the template parameter is given the code compiles and works.

@mike919192 mike919192 added the bug Something isn't working label Aug 17, 2023
@JohelEGP
Copy link
Contributor

This is resolved by #533.

@mike919192
Copy link
Author

Great! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants