Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数和内部函数的参数相同_C++_C++11_Arduino - Fatal编程技术网

C++ 函数和内部函数的参数相同

C++ 函数和内部函数的参数相同,c++,c++11,arduino,C++,C++11,Arduino,有人能帮助我如何实现我的函数只接受可以在其中调用函数的参数类型吗 我有一个Logger类,可以在Arduino代码的setup()函数中使用HardwareSerial对象启动它。 然后在loop()中,我想调用Logger.print()函数,该函数应该只接受可以调用的HardwareSerial.print()参数 这是我的丑陋和不工作的尝试: template <typename... ARGS> size_t print(const ARGS &... args) {

有人能帮助我如何实现我的函数只接受可以在其中调用函数的参数类型吗

我有一个Logger类,可以在Arduino代码的
setup()
函数中使用
HardwareSerial
对象启动它。
然后在
loop()
中,我想调用
Logger.print()
函数,该函数应该只接受可以调用的
HardwareSerial.print()
参数

这是我的丑陋和不工作的尝试:

template <typename... ARGS>
size_t print(const ARGS &... args) {
    if (serial != NULL) {
        if (sizeof...(args) == 2) {
            return this->serial->print(args[0], args[1]);
        } else if (sizeof...(args) == 1) {
            return this->serial->print(args[0]);
        }
    }
    return 0;
}

template <typename T>
size_t print(const T &t, typename std::enable_if<std::is_convertible<const __FlashStringHelper *, T>::value ||
                                                     std::is_base_of<const String &, T>::value ||
                                                     std::is_array<T>::value ||
                                                     //std::is_same<char[std::extent<T>::value], T>::value ||
                                                     std::is_same<char, T>::value ||
                                                     std::is_same<char *, T>::value ||
                                                     std::is_same<const char *, T>::value ||
                                                     std::is_same<unsigned char, T>::value ||
                                                     std::is_same<int, T>::value ||
                                                     std::is_same<unsigned int, T>::value ||
                                                     std::is_same<long, T>::value ||
                                                     std::is_same<unsigned long, T>::value ||
                                                     std::is_same<double, T>::value ||
                                                     std::is_convertible<const Printable &, T>::value ||
                                                     std::is_convertible<struct tm *, T>::value,
                                                 T>::type * = 0) {
    if (serial != NULL) {
        return this->serial->print(t);
    }
    retrun 0;
}
模板
打印尺寸(常量参数和…参数){
如果(串行!=NULL){
如果(sizeof…(args)==2){
返回此->序列->打印(参数[0],参数[1]);
}否则如果(sizeof…(args)==1){
返回此->序列->打印(参数[0]);
}
}
返回0;
}
模板
大小打印(常量t&t,类型名称std::启用(如果::类型*=0){
如果(串行!=NULL){
返回此->序列->打印(t);
}
重新运行0;
}

当您使用
decltype
进行SFINAE检查时,检测是否可以调用函数是小菜一碟:

template <typename... ARGS>
auto print(const ARGS &... args) -> decltype(this->serial->print(args...)) {
    if (serial != NULL) {
        return this->serial->print(args...);
    }
    return 0;
}
模板
自动打印(常量参数和…参数)->decltype(此->串行->打印(参数…){
如果(串行!=NULL){
返回此->序列->打印(参数…);
}
返回0;
}

可变模板扩展的语法为

template <typename... ARGS>
size_t print(const ARGS&... args) {
    if (serial != nullptr) {
        return this->serial->print(args...);
    }
    return 0;
}
模板
打印尺寸(常量参数和…参数){
如果(串行!=nullptr){
返回此->序列->打印(参数…);
}
返回0;
}
要使其友好,您可以使用:

template <typename... ARGS>
auto print(const ARGS&... args) -> decltype(this->serial->print(args...))
{
    if (serial != nullptr) {
        return this->serial->print(args...);
    }
    return 0;
}
模板
自动打印(常量参数和…参数)->decltype(此->串行->打印(参数…)
{
如果(串行!=nullptr){
返回此->序列->打印(参数…);
}
返回0;
}

由于您需要与
硬件序列
相同的接口,使用所有
打印
打印LN
写入
方法,因此
记录器
类应该继承自
打印
,然后只实现
写入(uint8\t)
只调用
硬件序列的方法
s
write(uint8\u t)

有关
Print
class的更多信息:

class Logger : public Print {
    public:
        virtual size_t write(uint8_t);
}
size_t Logger::write(uint8_t c) {
    if (this->serial != NULL) {
       return this->serial->write(c);
    }
    return 0;
}