4

Snippet:


#include <functional>

template <typename T>
struct CallableTrait;

template <typename R, typename... Args>
struct CallableTrait<std::function<R(Args...)>>
{
    using ReturnType = R;
};

template <typename Callable>
using CallableTraitT = CallableTrait<decltype(std::function{std::declval<Callable>()})>;

template <typename Callable>
auto test(Callable&&)
{
    using CallableInfo = CallableTraitT<Callable>;
    static_assert(!std::is_void_v<typename CallableInfo::ReturnType>);
}

int main()
{
    test([]() { return 42; });
    return 0;
}

Demo

This compiles fine with clang-12.0.0 and MSVC-19.16.27034, but gcc-11.0.0 throws an error:

prog.cc: In instantiation of 'auto test(Callable&&) [with Callable = main()::<lambda()>]':
prog.cc:25:29:   required from here
prog.cc:20:25: error: invalid use of incomplete type 'struct CallableTrait<main()::<lambda()> >'
   20 |     static_assert(!std::is_void_v<typename CallableInfo::ReturnType>);
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:5:8: note: declaration of 'struct CallableTrait<main()::<lambda()> >'
    5 | struct CallableTrait;
      |    

Who is right and who isn't?

EDIT: Bug is tracked here gcc-bugzilla

10
  • []() { return 42; }std::function<int()>.
    – Eljay
    Nov 30, 2020 at 18:58
  • What you want is basically what I do here: stackoverflow.com/a/53673648/4342498 Nov 30, 2020 at 19:02
  • 1
    @NathanOliver: Thx, but the question was why 2 of 3 compilers are good, while gcc thinks this is wrong.
    – Juergen
    Nov 30, 2020 at 19:06
  • @Eljay: but CallableTraitT "calls" std::function...
    – Jarod42
    Nov 30, 2020 at 19:33
  • And indeed, adding std::function makes gcc happy Demo.
    – Jarod42
    Nov 30, 2020 at 19:34

1 Answer 1

0

The issue is tracked here gcc-bugzilla

3
  • 1
    I don't think so. Jonathan just reduced the example, it's not been officially Confirmed yet.
    – cigien
    Dec 1, 2020 at 13:53
  • @cigien: my bad. I am not aware of the semantics in bugzilla. Will make a defensive edit.
    – Juergen
    Dec 1, 2020 at 13:56
  • No worries. Thanks for filing the bug report. It does look like a bug to me, for what that's worth :)
    – cigien
    Dec 1, 2020 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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