Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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++_Templates_Metaprogramming_Member - Fatal编程技术网

C++ C++;模板元编程成员函数循环展开

C++ C++;模板元编程成员函数循环展开,c++,templates,metaprogramming,member,C++,Templates,Metaprogramming,Member,我刚刚开始在代码中使用模板元编程。我有一个类,它的成员是多维笛卡尔点的向量。下面是该类的基本设置: template<size_t N> class TGrid{ public: void round_points_3(){ for(std::size_t i = 0; i < Xp.size();i++){ Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbin

我刚刚开始在代码中使用模板元编程。我有一个类,它的成员是多维笛卡尔点的向量。下面是该类的基本设置:

template<size_t N>
class TGrid{
public:
     void round_points_3(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
             Xp[i][1] = min[1] + (std::floor((Xp[i][1] - min[1]) * nbins[1] / (max[1] - min[1])) * bin_w[1]) + bin_w[1]/2.0;
             Xp[i][2] = min[2] + (std::floor((Xp[i][2] - min[2]) * nbins[2] / (max[2] - min[2])) * bin_w[2]) + bin_w[2]/2.0;
         }
    }
    void round_points_2(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
             Xp[i][1] = min[1] + (std::floor((Xp[i][1] - min[1]) * nbins[1] / (max[1] - min[1])) * bin_w[1]) + bin_w[1]/2.0;
         }
    }
    void round_points_1(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
         }
    }
public:

std::vector<std::array<double,N> > Xp;
std::vector<double> min, max, nbins, bin_w;
};
模板
类TGrid{
公众:
无效圆_点_3(){
对于(std::size_t i=0;i

此类表示多维网格。维度由模板值N指定。I将有许多操作,这些操作可以通过根据特定维度定制模板特定的成员函数(如循环展开)来提高效率

在类TGrid中,我有3个特定于维度D=1、D=2和D=3的函数。这由函数的下标_1、_2和_3表示

我正在寻找一种面向模板元编程的方法来编写 这三个功能更加紧凑


我已经看到了循环展开的例子,但是所有这些例子都不考虑模板类的成员函数。 把这是否是一个适当的优化,或者是否应该首先考虑其他优化的问题放在一边,这就是我将如何做的。(但我确实同意,有时显式展开循环显然更好——编译器并不总是最好的判断。)

不能部分地专门化成员函数,也不能在不专门化外部结构的情况下专门化嵌套结构,因此唯一的解决方案是为展开机制使用单独的模板结构。可以将其放在其他名称空间中:)

展开实现:

template <int N>
struct sequence {
    template <typename F,typename... Args>
    static void run(F&& f,Args&&... args) {
        sequence<N-1>::run(std::forward<F>(f),std::forward<Args>(args)...);
        f(args...,N-1);
    }
};

template <>
struct sequence<0> {
    template <typename F,typename... Args>
    static void run(F&& f,Args&&... args) {}
};
模板
结构序列{
模板
静态无效运行(F&&F,参数&&…参数){
序列::run(std::forward(f),std::forward(args)…);
f(args…,N-1);
}
};
模板
结构序列{
模板
静态无效运行(F&&F,Args&&…Args){}
};
这将获取任意函数对象和参数列表,然后使用参数和附加的最终参数调用对象N次,其中最终参数的范围为0到N-1。通用参考和可变模板是不必要的;同样的思想也可以在C++98中使用,但通用性较差

round_points
然后使用helper静态成员函数调用
sequence::run

template <size_t N>
class TGrid {
public:
    template <size_t K>
    void round_points(){
        for (std::size_t i = 0; i < Xp.size();i++) {
            sequence<K>::run(TGrid<N>::round_item,*this,i);
        }
    }

    static void round_item(TGrid &G,int i,int j) {
        G.Xp[i][j] = G.min[j] + (std::floor((G.Xp[i][j] - G.min[j]) * G.nbins[j] / (G.max[j] - G.min[j])) * G.bin_w[j]) + G.bin_w[j]/2.0;
    }

    // ...
};
模板
类TGrid{
公众:
模板
无效圆点(){
对于(std::size_t i=0;i
编辑:附录

对于编译器来说,使用指向成员函数的指针进行等效似乎很难内联。作为替代方案,为了避免使用静态round_项,您可以使用lambda,例如:

template <size_t N>
class TGrid {
public:
    template <size_t K>
    void round_points(){
        for (std::size_t i = 0; i < Xp.size();i++) {
            sequence<K>::run([&](int j) {round_item(i,j);});
        }
    }

    void round_item(int i,int j) {
        Xp[i][j] = min[j] + (std::floor((Xp[i][j] - min[j]) * nbins[j] / (max[j] - min[j])) * bin_w[j]) + bin_w[j]/2.0;
    }

    // ...
};
模板
类TGrid{
公众:
模板
无效圆点(){
对于(std::size_t i=0;i
不要这样做。使用通用的
圆点和第二个循环
array::size()
是constexpr,然后编译器将自行决定展开循环是否是一个好主意。“我将有许多操作,通过根据特定维度定制特定于模板的成员函数(如循环展开),可以提高效率。”。哦,是的。测量。我测量了其他操作(边缘化等)的时间,这些小事情往往会产生影响。原因是我正在处理高达4GB的网格。谢谢你的回答,我会试试这个。还感谢您回答了这个问题,但没有讨论它是否是一个适当的优化。为什么将圆形项作为静态项?原因有两个:第一,它的代码更清晰,没有使用指向成员函数对象的指针或std::mem_fn;但是第二,这使得编译器的内联更加容易。另一种方法是将round_项保持为非静态,但将lambda传递给sequence::run()。我将根据需要编辑答案。