2

For the following code:

#include <iostream>
#include <vector>

template <class T>
inline auto foo(const T* A)
{
    return A[0];
}

template <class T>
struct impl
{
    template <typename F>
    static auto bar(const T& A, F func)
    {
        return func(&A[0]);
    }
};

int main()
{
    std::vector<size_t> A = {2, 3, 4};
    std::cout << impl<decltype(A)>::bar(A, &foo) << std::endl;
    return 0;
}

I get (with C++14 using clang on macOS) that

main.cpp:23:18: error: no matching function for call to 'bar'
    std::cout << impl<decltype(A)>::bar(A, &foo) << std::endl;
                 ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:14:17: note: candidate template ignored: couldn't infer template argument 'F'
    static auto bar(const T& A, F func)
                ^
1 error generated.

My question is: Why? How can I fix it?

3 Answers 3

3

foo is the name of a template, not the name of a function. bar requires a type, not a template. Fortunately lambda expressions are a fast easy way to wrap generic function into an object type that can be known and do the dispatch for you. That would give you

std::cout << impl<decltype(A)>::bar(A, [](const auto& var){ return foo(var); }) 
          << std::endl;
0
2

The issue is actually from foo not from impl. foo is a template and you will an error also for:

auto x = &foo;

source>:23:15: error: unable to deduce 'auto' from '& foo'
   23 |     auto x = &foo;
      |               ^~~
<source>:23:15: note:   couldn't deduce template parameter 'auto'

I am not suggesting this a the "right" solution, it is just to show how in principle you can get a pointer to the right instantiation of foo:

int main()
{
    std::vector<size_t> A = {2, 3, 4};
    std::cout << impl<decltype(A)>::bar(A,
        static_cast<size_t(*)(const decltype(A)::value_type*)>(&foo)) << std::endl;
    return 0;
}

Or the more readable (thanks to @StoryTeller):

int main()
{

    std::vector<size_t> A = {2, 3, 4};
    //auto x = &foo<decltype(A)::value_type>;
    std::cout << impl<decltype(A)>::bar(A,&foo<decltype(A)::value_type>) << std::endl;
    return 0;
}

Note that gcc does emit the same error as for the original. This appears to be a bug in gcc. Removing the // from the line above magically makes it compile also with gcc even though x is not used. The bug seems to be fixed in gcc-trunk: https://godbolt.org/z/noqGEP (see also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54111).

12
  • 3
    If we are gonna spell out the argument type involved, &foo<decltype(A)::value_type> is a whole lot more readable. And now it doesn't even need a cast (even a C++-style one) Dec 7, 2020 at 15:01
  • 1
    @TomdeGeus Nathans solution is a lot more flexible because you don't have to explicitly spell out the type but rather let auto and overload resolution do that for you. Dec 7, 2020 at 15:11
  • 1
    @TomDeGeus see edit. I dont know msvc, but I got the same error with gcc (not with clang). I believe it is a bug in gcc and adding the line auto x = &foo<decltype(A)::value_type>; magically fixes the error (even though x is not used) Dec 7, 2020 at 15:55
  • 1
    @TomdeGeus - I'm guessing GCC? I encountered this bug myself in the past (here's a report, though not by me gcc.gnu.org/bugzilla/show_bug.cgi?id=97999). I was hoping it'd by fixed by GCC 10. Judging by the godbolt link... seems not. Dec 7, 2020 at 15:57
  • 2
    Here's a report from 2012 gcc.gnu.org/bugzilla/show_bug.cgi?id=54111 ... oh well Dec 7, 2020 at 15:58
1

This should work (without lambda):

#include <iostream>
#include <vector>

template <class T>
auto foo(const T* A)
{
    return A[0];
}

template <class T>
struct impl
{
    template <typename F>
    static auto bar(const T& A, F func)
    {
        return func(&A[0]);
    }
};

int main()
{
    std::vector<size_t> A = {20, 3, 4};

    std::cout << impl<decltype(A)>::bar(A, &foo<size_t>) << std::endl;

    return 0;
}

but gcc fails for some reason (clang and msvc are fine with that).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.