简体   繁体   English

如何正确使用 c++ void_t?

[英]How to use c++ void_t correctly?

I am trying to practice on void_t usage but the following code gives a compilation error.我正在尝试练习void_t用法,但以下代码给出了编译错误。 is_fun is typedef in CompS struct so I think Comp::is_fun should be valid. is_funCompS结构中的 typedef,所以我认为Comp::is_fun应该是有效的。

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

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>>
{
    // ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM