0

When i use following as say Event<void()> it works fine. But now i like to have default template value so i change following to template<class _Fty = void()> so i can have Event<> aEvent. But it seem to fail with bunch of errors.

template<class _Fty>
struct Event final {
    typedef std::function<_Fty> listener_type;
    ...
}

Can anyone suggest how can accomplish what i want as describe above?

error C2027: use of undefined type 'std::_Get_function_impl<_Fty>'
        with
        [
            _Fty=void (__cdecl *)(void)
        ]
C:\PROGRA~2\MIB055~1\2017\Professional\VC\Tools\MSVC\14.16.27023\include\functional(1479): note: see declaration of 'std::_Get_function_impl<_Fty>'
        with
        [
            _Fty=void (__cdecl *)(void)
        ]
particle
  • 3,280
  • 4
  • 27
  • 39
  • 1
    `template` --> `template`. Also, names like `_Fty` are reserved for the implementation and you shouldn't use them. – cigien Aug 12 '20 at 17:45
  • @cigien Gives different error `error C2027: use of undefined type std::_Get_function_impl<_Fty>' with [ _Fty=void ]` – particle Aug 12 '20 at 17:48
  • Ok, then please make a [mre] – cigien Aug 12 '20 at 17:49
  • 4
    Unrelated note: Any identifier starting with an underscore followed by an uppercase letter is reserved in C++. This shouldn't be related to your issue, but you should consider renaming the template parameter to just `Fty` instead of `_Fty` – Human-Compiler Aug 12 '20 at 17:50
  • 1
    [Works fine here](http://coliru.stacked-crooked.com/a/e289623ba23631ac). Show your complete code as this example is not enough to reproduce the problem. – cdhowie Aug 12 '20 at 17:51
  • 3
    You might as well know all the underscore rules, so [here's a link to a broader explanation](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) of the problem Human-Compiler raised. – user4581301 Aug 12 '20 at 17:56
  • You say you defined the default to be `_Fty = void()`, but the error message says you wrote `_Fty = void (*)()`. What you said you did would have been correct, but what you appear to have done is not. [Take a look](https://godbolt.org/z/bKzcj5). – HTNW Aug 12 '20 at 17:59
  • 1
    @Human-Compiler _Fty funny as it is, it might. Apparently _Fty is a used identifier in at least one compiler: https://developercommunity.visualstudio.com/content/problem/165660/error-c2027-use-of-undefined-type-std-get-function.html – SergeyA Aug 12 '20 at 18:00
  • @SergeyA Ha, nice, that explains the OP's error message :) Might be the first time I've seen a reserved name actually conflict. Sounds like an answer. – cigien Aug 12 '20 at 18:02
  • @Human-Compiler Thanks for the info about underbar. – particle Aug 12 '20 at 18:02
  • @HTNW on the online compile pasting my code does not give any error and i do have that `void()`. It compile fine. The exact code fail to compile on my box. I have VS2017. – particle Aug 12 '20 at 18:17
  • See, the comments. Using `_Fty` invokes undefined behavior. Change the name. – cigien Aug 12 '20 at 18:18
  • Thank you guys, at least i know nothing is wrong with the code. I can figure out rest on my own. – particle Aug 12 '20 at 18:22

0 Answers0