如何正确使用C ++ void_t?

BK *_* C. 0 c++ c++17 void-t

我正在尝试void_t使用用法,但是以下代码给出了编译错误。is_funCompSstruct 中的typedef,所以我认为Comp::is_fun应该是有效的。

我有什么想念的吗?

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;
}
Run Code Online (Sandbox Code Playgroud)

错误:

   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
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 5

您正在尝试声明一个新的主模板,但是您需要的是专业化:

template <typename T, typename Comp>
class my_set<T, Comp, void_t<typename Comp::is_fun>>
{
    // ...
Run Code Online (Sandbox Code Playgroud)