0

I am trying to practice on void_t usage but the following code gives a compilation error. is_fun is typedef in CompS struct so I think Comp::is_fun should be valid.

Is there anything I missed here?

template <typename T, typename Comp, typename = void_t<>>
class my_set
{
    public:
        my_set() : mem(5){}
        T mem;
};

template <typename T, typename Comp, void_t<typename Comp::is_fun> >
class my_set 
{
    public:
        my_set() : mem(10){}
        T mem;
};

struct CompS
{
    typedef int is_fun;
};

int main()
{
    my_set<int, CompS> a;
    std::cout << a.mem << std::endl;


    return 0;
}

Error:

   voidt.cpp:17:38: error: ‘void’ is not a valid type for a template non-type parameter
     template <typename T, typename Comp, void_t<typename Comp::is_transparent> >
                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    voidt.cpp:9:38: error: template parameter ‘class<template-parameter-1-3>’
     template <typename T, typename Comp, typename = void>
                                      ^~~~~~~~
    voidt.cpp:18:7: error: redeclared here as ‘<typeprefixerror><anonymous>’
     class my_set
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
BK C.
  • 573
  • 2
  • 7
  • 16
  • 4
    Please tag only with the C++ version you are actually using. I guess that's C++17 since `std::void_t` didn't exist before then. – Quentin Sep 26 '19 at 14:37
  • 1
    Not your problem but... `std::void_t<>` is `void`; so, as default type parameter in the main `my_set`, you can write `typename = void` instead of `typename = std::void_t<>` – max66 Sep 26 '19 at 18:14

1 Answers1

5

You're trying to declare a new primary template, but what you need is a specialization:

template <typename T, typename Comp>
class my_set<T, Comp, void_t<typename Comp::is_fun>>
{
    // ...
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • …And note that many versions of Clang are not happy with more than one specialization using `void_t` in the same place: it considers them all equivalent and thus invalid to repeat. (It’s not clear whether it’s correct in that assessment.) – Davis Herring Sep 27 '19 at 02:15
  • @DavisHerring Hmm, alright... is there a workaround, or discussion somewhere about that quirk? – Quentin Sep 27 '19 at 08:53
  • It’s [been discussed here](https://stackoverflow.com/q/45948990/8586227). It affects [current versions](https://godbolt.org/z/TX5L6n) of Clang and ICC; the same workaround works for both. – Davis Herring Sep 27 '19 at 13:33