Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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_Variadic Templates - Fatal编程技术网

C++ 具有模板函数名、传递参数和返回值的可变模板

C++ 具有模板函数名、传递参数和返回值的可变模板,c++,c++11,variadic-templates,C++,C++11,Variadic Templates,根据这个问题,我一直在尝试创建一个模板函数,该函数调用其mixin的所有相同命名方法。这已在上一个问题中完成并验证 现在我尝试获取SensorType的返回值:: 分析: #include<iostream> #include <string> struct EdgeSensor { void update(int i) { std::cout << "EdgeSensor::update " << i << std::en

根据这个问题,我一直在尝试创建一个模板函数,该函数调用其mixin的所有相同命名方法。这已在上一个问题中完成并验证

现在我尝试获取SensorType的返回值::

分析:

#include<iostream>
#include <string>

struct EdgeSensor
{
    void update(int i) { std::cout << "EdgeSensor::update " << i <<  std::endl; }
    void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "EdgeSensor::printStats" << std::endl; 
                               return std::string("EdgeSensor::printStats"); }
};

struct TrendSensor
{
    void update(int i ) { std::cout << "TrendSensor::update" << i << std::endl; }
    void updat2(const int i ) { std::cout << "TrendSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "TrendSensor::printStats" << std::endl; 
                               return std::string("TrendSensor::printStats"); }
};

template <class T, void (T::*)(const int)>
struct Method { };

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
   template <class T, void(T::*M)(const int)>
   int runSingle(Method<T, M> , const int i) {
      (this->*M)(i);
      return 0;
   }

   template <class... Ts>
   void runAll(const int i) {
      int run[sizeof...(Ts)] = { runSingle(Ts{},i)... };
      (void)run;
   }

public:
    void update() {
       runAll<Method<SensorType, &SensorType::update>...>(2);
    }
    void updat2() {
       const int k = 3;
       runAll<Method<SensorType, &SensorType::updat2>...>(k);
    }
    void printStats() {
    //   runAll<Method<SensorType, &SensorType::printStats>...>();
    }
};

int main() {
  {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }

  {
    BaseSensor<EdgeSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }
}
这不符合我的说法

g++ -Wall -Wextra -g -std=c++11   -c -o "obj_dbg/main.opp" "main.cpp"
main.cpp: In instantiation of ‘void BaseSensor<SensorType>::printStats() [with SensorType = EdgeSensor, TrendSensor]’:
main.cpp:65:20:   required from here
main.cpp:58:8: error: could not convert template argument ‘&EdgeSensor::printStats’ to ‘void (EdgeSensor::*)()’
make: *** [obj_dbg/main.opp] Error 1
g++-Wall-Wextra-g-std=c++11-c-o“obj_dbg/main.opp”“main.cpp”
main.cpp:在“void BaseSensor::printStats()[with SensorType=EdgeSensor,TrendSensor]”的实例化中:
main.cpp:65:20:从这里开始需要
main.cpp:58:8:错误:无法将模板参数“&EdgeSensor::printStats”转换为“void(EdgeSensor::*)()”
make:**[obj_dbg/main.opp]错误1

那么,我如何从
SensorType::printStats()
中获取返回值呢?

这里是您对代码的审阅,以便它能够按要求工作:

#include<iostream>
#include <string>
#include <vector>

struct EdgeSensor
{
    void update(int i) { std::cout << "EdgeSensor::update " << i <<  std::endl; }
    void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "EdgeSensor::printStats" << std::endl; 
    return std::string("EdgeSensor::printStats"); }
};

struct TrendSensor
{
    void update(int i ) { std::cout << "TrendSensor::update" << i << std::endl; }
    void updat2(const int i ) { std::cout << "TrendSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "TrendSensor::printStats" << std::endl; 
    return std::string("TrendSensor::printStats"); }
};

template<typename ... SensorType>
class BaseSensor : public SensorType ... {
    template<typename F>
    struct Invoke;

    template<typename R, typename... A>
    struct Invoke<R(A...)> {
        template <R(SensorType::* ...M)(A...), typename T>
        static std::vector<R> run(T *t, A... args) {
            std::vector<R> vec;
            int arr[] = { (vec.push_back((t->*M)(args...)), 0)... };
            (void)arr;  
            return vec;
        }
    };

    template<typename... A>
    struct Invoke<void(A...)> {
        template <void(SensorType::* ...M)(A...), typename T>
        static void run(T *t, A... args) {
            int arr[] = { ((t->*M)(args...), 0)... };
            (void)arr;  
        }
    };

public:
    void update() {
       Invoke<void(int)>::template run<&SensorType::update...>(this, 2);
    }
    void updat2() {
       const int k = 3;
       Invoke<void(int)>::template run<&SensorType::updat2...>(this, k);
    }
    void printStats() {
         auto vec = Invoke<std::string(void)>::template run<&SensorType::printStats...>(this);
         for(auto &&v: vec) {
             std::cout << "--->" << v << std::endl;
        }
    }
};

int main() {
  {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }

  {
    BaseSensor<EdgeSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }
}
#包括
#包括
#包括
结构边缘传感器
{

voidupdate(inti){std::cout将解决方案扩展到您可以执行的任何类型的成员函数(实际上,在考虑c++11限制的情况下对其进行了一些简化)。该方法解析成员函数的类型,以便能够推断其结果类型。它还使用
InferOwnerType
来推断mixin类型,并避免直接传递静态浇铸的
指针。根据成员函数的结果,现在我们可以将其存储到数组中,或者使用
int
数组的技巧确保调用了每个成员函数

#include <iostream> // std::cout std::endl
#include <string>   // std::string
#include <utility>  // std::declval

struct EdgeSensor //a mixin
{
    void update(int a){ std::cout << "EdgeSensor::update" << a << std::endl; }
    std::string updat2(int const v) { return "EdgeSensor::printStats"; }
};

struct TrendSensor //another mixin
{
    void update(int a){ std::cout << "TrendSensor::update" << std::endl; }
    std::string updat2(int const v) { return "TrendSensor::printStats"; }
};

template <class Res, class This, class... Args>
This InferOwnerType(Res (This::*foo)(Args...)) { }

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
    template <class M, class... Args>
    auto run(M m, Args... args)
       -> decltype((std::declval<decltype(InferOwnerType(m))*>()->*m)(args...)) {
       return (static_cast<decltype(InferOwnerType(m))*>(this)->*m)(args...);
    }

public:
    template <class... Args>
    void update(Args... args) {
       int arr[] = {(run(&SensorType::update, args...), 0)...};
       (void)arr;
    }
    template <class... Args>
    void updat2(Args... args) {
       std::string s[] = {run(&SensorType::updat2, args...)...};
       for (int i = 0; i < sizeof...(SensorType); i++)
          std::cout << s[i] << std::endl;
    }
};

int main() {
   BaseSensor<EdgeSensor, TrendSensor> bs;
   bs.update(4);
   bs.updat2(0);
   BaseSensor<EdgeSensor> bs2;
   bs2.update(1);
   bs2.updat2(0);
}
#包括//std::cout std::endl
#include//std::string
#include//std::declval
结构边缘传感器//混音器
{

voidupdate(inta){std::cout不确定是否可以使用c++11,如果可以,那么我认为这是最简单的

#include <iostream>
#include <string>

struct EdgeSensor
{
    void update(int i) { std::cout << "EdgeSensor::update " << i <<  std::endl; }
    void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "EdgeSensor::printStats" << std::endl; 
                               return std::string("EdgeSensor::printStats"); }
};

struct TrendSensor
{
    void update(int i ) { std::cout << "TrendSensor::update" << i << std::endl; }
    void updat2(const int i ) { std::cout << "TrendSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "TrendSensor::printStats" << std::endl; 
                               return std::string("TrendSensor::printStats"); }
};

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
public:
    void update() {
       auto v = { (static_cast<SensorType*>(this)->update(1), 0)... }; // *
       (void) v;
    }
    void updat2() {
       const int k = 3;
       auto v = { (static_cast<SensorType*>(this)->updat2(k), 0)... }; // *
       (void) v;
    }
    void printStats() {
       auto v = { static_cast<SensorType*>(this)->printStats()... };
       for (auto s : v) {
           std::cout << s << std::endl;
       }
    }
};

int main() {
  {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }

  {
    BaseSensor<EdgeSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }
}
#包括
#包括
结构边缘传感器
{

void update(int i){std::cout Nice!!gcc扩展正在推断数组类型,对吗?
auto v={(this->SensorType::update(1),0)…};
可能会更好(允许避免可能的虚拟分派错误,这在成员指针方法中无法实现)@W.F.不,这是标准的一部分,但是这里不使用的是
操作符,它是一个GCC扩展,意思是在
之前应用表达式,丢弃结果,然后在
之后应用表达式并使用它的值。因此,您有一个带有n0s的初始值设定项列表,您好,不幸的是上面的e并没有编译。我一直在试图找出原因,但也许你可以更详细地识别它easily@nass它是用GCC编译的,但不是用GCC编译的。我创建了一个工具来探索这是GCC还是代码的问题,但不幸的是还没有答案。我会回来的,只要我有一些消息给你。啊,是的,我确实看到了,并且一直在关注it:)也许将此信息添加为答案的一部分会很有用(至少在提供解决方案之前)@nass这是一个非常有趣的错误,我很想说代码是正确的,但我不确定扩展是否100%有效。-)嗨,再一次,在试图理解发生了什么的时候,我有几个问题:)我们可以在聊天窗口中谈论它们吗?
InferThisType
有点疯狂,但也很有趣-D@skypj确认正确的措辞不是很好也许
InferOwnerClass
是一个更好的选择…@skypjack我以某种方式阅读了你的评论,我相信我对它进行了重新解释…你的意思是将成员指针视为要发送的say标记有点疯狂:)我承认我有点疯狂(~o~)
#include <iostream>
#include <string>

struct EdgeSensor
{
    void update(int i) { std::cout << "EdgeSensor::update " << i <<  std::endl; }
    void updat2(const int i ) { std::cout << "EdgeSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "EdgeSensor::printStats" << std::endl; 
                               return std::string("EdgeSensor::printStats"); }
};

struct TrendSensor
{
    void update(int i ) { std::cout << "TrendSensor::update" << i << std::endl; }
    void updat2(const int i ) { std::cout << "TrendSensor::updat2" << i << std::endl; }
    std::string printStats() { std::cout << "TrendSensor::printStats" << std::endl; 
                               return std::string("TrendSensor::printStats"); }
};

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
public:
    void update() {
       auto v = { (static_cast<SensorType*>(this)->update(1), 0)... }; // *
       (void) v;
    }
    void updat2() {
       const int k = 3;
       auto v = { (static_cast<SensorType*>(this)->updat2(k), 0)... }; // *
       (void) v;
    }
    void printStats() {
       auto v = { static_cast<SensorType*>(this)->printStats()... };
       for (auto s : v) {
           std::cout << s << std::endl;
       }
    }
};

int main() {
  {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }

  {
    BaseSensor<EdgeSensor> ets;
    ets.update();
    ets.updat2();
    ets.printStats();
  }
}