1

Related to this

Can't get the following to compile and I don't really understand why.

Codebolt Code

Snippet here

#include <vector>
#include <string>



template<typename T>
class A
{
    using func_type = bool(int const&);

    template<func_type U, func_type X>
    [[using gnu:cold]]void example(std::vector<std::string>&&);
};


template <typename T>
template <typename A<T>::func_type U, typename A<T>::func_type X>
void A<T>::example(std::vector<std::string>&&)
{

}

Thank you

eucristian
  • 391
  • 3
  • 17
  • man this is some wacky template stuff. i'm not sure if something like this is possible, but even then, it's really overly-complicated. maybe if you could tell us what you want from these we could offer a better solution? – tb044491 May 18 '20 at 19:50
  • personally, i have always had trouble with declaring class member functions with templates outside of the classes. aside from overloading the "T" class template, i'm not sure this is the way to go, but maybe i just don't know enough. – tb044491 May 18 '20 at 19:57
  • Can't you move this line `using func_type = bool(int const&);` outside of the class? It will solve your issue. – Vahid Noormofidi May 18 '20 at 20:04
  • @VahidNoormofidi wouldn't really want to do it as it would pollute the namespace, that is including other stuff as well. I am trying to define a template function outside the class – eucristian May 18 '20 at 20:09

1 Answers1

0

If you don't want to use the func_type anywhere outside of your class. Then you should change it to

using func_type = bool(*)(int const&);

Remember that the syntax is similar to emplying typedef for function pointers.

Vahid Noormofidi
  • 748
  • 10
  • 17
  • 1
    Interesting. Now the only curiosity remaining is *why* this makes the code well-formed – StoryTeller - Unslander Monica May 18 '20 at 20:18
  • 1
    As I said, `using` and `typedef` both are almost identical. For the `typedef` the usage is `typedef bool (*func_type)(int const&)` and you should comply with the typedef function pointer format. – Vahid Noormofidi May 18 '20 at 20:28
  • 1
    What you said is immaterial. The original code fails irregardless of whether or not we use `typedef` or `using` to define the type as a function type. What matters is that it's defined as a *pointer*. You haven't explained **at all** why changing to a pointer fixes the error. – StoryTeller - Unslander Monica May 18 '20 at 20:30
  • 1
    [`Array and function types may be written in a template declaration, but they are automatically replaced by pointer to object and pointer to function as appropriate.`](https://en.cppreference.com/w/cpp/language/template_parameters) so in theory it shouldn't make any difference? Clang and MSVC show the same behaviour so I don't think it's a bug – Alan Birtles May 18 '20 at 20:39
  • The code doesn't fail if you even use `typedef bool (* func_type)(int const&)` but writing it in `using func_type = bool(int const&)` is like writing `typedef bool func_type(int const&)` which honestly I don't know if any compiler accepts it. – Vahid Noormofidi May 18 '20 at 20:42
  • 1
    Ah maybe the `in a declaration` bit is the key, doesn't apply in definitions? This works https://godbolt.org/z/AvNZpk – Alan Birtles May 18 '20 at 20:42
  • @AlanBirtles might it be because of some aggressive and weird inlining? – Vahid Noormofidi May 18 '20 at 20:45
  • @AlanBirtles i see what you mean, so you suspect that it is only in declarations, but in definitions it is up to you to provide the correct type. – eucristian May 18 '20 at 20:58
  • 1
    Maybe, seems odd though that it works when `A` isn't a template. It is possible that gcc, clang and MSVC all have the same bug – Alan Birtles May 18 '20 at 21:03