小编Tho*_*man的帖子

公共继承的静态断言

我构建了一个帮助类,它将通过模板构建一个自定义类,这个自定义类必须从某个类继承,我可以检查这个std::is_base_of.

但是我还需要检查继承是否公开,如何实现?

作为参考,这里是一个精简版的课程,我std::is_base_of在那里.

template<class CustomSink>
class Sink
{
    static_assert(std::is_base_of<BaseSink, CustomSink>::value, "CustomSink must derive from BaseSink");
    //Some static assert here to check if custom sink has publicly inherited BaseSink 
    //static_assert(is_public.....
public:
    template<class... Args>
    Sink(Args&&... args)
    {
    }
    ~Sink()
    {
    }       
};
Run Code Online (Sandbox Code Playgroud)

c++ static-assert type-traits c++11

13
推荐指数
2
解决办法
2060
查看次数

从可变参数模板构造类

我正在尝试创建一个名为class的类Sink,它将创建一个指向您传入的类的指针,这是在RAII中包装api.

在此代码的完整版本中,自定义类也从另一个类继承,并且有静态资产来检查它.指针也传递给api.

但为了保持简单,我删除了这个.

这是我从cpp.sh得到的错误

 In function 'int main()':
43:30: error: no matching function for call to 'Sink<OneArg>::Sink(int)'
43:30: note: candidate is:
10:5: note: Sink<CustomSink, Args>::Sink(Args&& ...) [with CustomSink = OneArg; Args = {}]
10:5: note:   candidate expects 0 arguments, 1 provided
Run Code Online (Sandbox Code Playgroud)

码:

#include <string>
#include <iostream>
#include <memory>
#include <utility>

template<typename CustomSink, typename... Args>
class Sink
{
public:
    Sink(Args&&... args)
    {        
       _ptr = std::make_unique<CustomSink>(std::forward<Args>(args)...);        
    }
    ~Sink() 
    {
    }
private:
    std::unique_ptr<CustomSink> _ptr;
};


//////////////////////////////////////////////////////////////////////
class NoArg
{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates

4
推荐指数
2
解决办法
154
查看次数