Open
Description
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.