我希望可以只抛出一段我不明白为什么它如此运行的代码是可以的。

以下代码有两个问题。

1)为什么两个实例的this指针显示相同的值?下面是程序的输出:

WJ::WJ(Jit&)
this = 0x7ffff1743950 JV<T, N>::JV(Jit&, indices<Is ...>) [with int ...Is = {0}; T = WJ<float>; int N = 1]
RS<T>::RS(Jit&) [with T = WJ<float>]
this = 0x7ffff1743950 JV<T, N>::JV(Jit&, indices<Is ...>) [with int ...Is = {0}; T = RS<WJ<float> >; int N = 1]
PS<T>::PS(Jit&) [with T = RS<WJ<float> >]
go for it
ptr = 0x7ffff1743950 JV<T, N>::JV(Jit&, JV<T, N>*, indices<Is ...>) [with int ...Is = {0}; T = RS<WJ<float> >; int N = 1]
PS<T>::PS(const PS<T>&) [with T = RS<WJ<float> >; PS<T> = PS<RS<WJ<float> > >]

它显示值0x7ffff1743950的2倍。这使我感到惊讶,因为我确信在创建第二个实例之前,第一个实例不会被销毁。

2)我尝试将PS设置为原始的orig_ptr的深拷贝。这里使用的设置是递归模板实例化设置。因此,每个级别上的orig_ptr应该尊重层次结构并相应地指向。我不明白的是,为什么代码使用F{{(void(Is),j,ptr->F[Is])...}}编译(在代码中标记),为什么不使用F{{(void(Is),j,&ptr->F[Is])...}}编译? (我认为是正确的)。我看不到编译器正在调用哪个T(即RS<WJ<float> >)构造函数。没有RS<WJ<float> >::RS(Jit&,RS<WJ<float> >),仅存在指针版本。
#include<iostream>
#include<array>

struct Jit {};


template <int... Is>
struct indices {};

template <int N, int... Is>
struct build_indices
  : build_indices<N-1, N-1, Is...> {};

template <int... Is>
struct build_indices<0, Is...> : indices<Is...> {};


template<class T,int N>
struct JV {

  JV(Jit& j) : JV(j,build_indices<N>{}) {}
  template<int... Is>
  JV(Jit& j, indices<Is...>) :
    jit(j), F{{(void(Is),j)...}} {
    std::cout << "this = " << (void*)this << " " << __PRETTY_FUNCTION__ << "\n";
  }


  JV(Jit& j,JV<T,N>* ptr) : JV(j,ptr,build_indices<N>{}) {}
  template<int... Is>
  JV(Jit& j,JV<T,N>* ptr, indices<Is...>) :
    // Why does this not compile with &ptr->F[Is] ??
    // What is it calling now (there is no T::T(Jit&,sub_T))
    jit(j), orig_ptr(ptr), F{{(void(Is),j,ptr->F[Is])...}} {
    std::cout << "ptr = " << (void*)ptr << " " << __PRETTY_FUNCTION__ << "\n";
  }
  std::array<T,N> F;
  JV<T,N>* orig_ptr;
  Jit& jit;
};

template<class T>
struct RS : public JV<T,1>
{
  RS(Jit &j): JV<T,1>(j) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }

  RS(Jit &j, RS* orig): JV<T,1>(j,orig) {
    std::cout << "orig = " << orig << " " << __PRETTY_FUNCTION__ << "\n";
  }
};

template<class T>
struct PS : public JV<T,1>
{
  PS(Jit& j): JV<T,1>(j) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }

  PS(const PS& rhs) : JV<T,1>(rhs.jit,const_cast<JV<T,1>*>(  static_cast<const JV<T,1>*>(&rhs) ) ) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

template<class T>
struct WJ
{
  WJ(Jit& j) {
    std::cout << "WJ::WJ(Jit&)\n";
  }
  WJ(Jit& j,WJ* ptr) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};

int main() {
  Jit j;
  PS<RS<WJ<float> > > w(j);
  std::cout << "go for it\n";
  PS<RS<WJ<float> > > wcopy(w);
}

**编辑**

我在WJ中添加了指针接口(interface),以便F的实例化过程可以完全递归。我认为可能是由于SFINE。但事实并非如此。

在g++-4.7(Ubuntu / Linaro 4.7.2-4precise1)4.7.2和-O0中进行了尝试。

**编辑**

@sehe的回答为我指明了正确的方向。当然,第二种void(Is)构造函数不需要JV。仅在第一种类型中,它用于模拟std::fill。但是我们在初始化列表中!序列运算符的默认实现有助于完全消除(void)Is

现在,在第二种情况下,还有Is的另一种用法,即ptr->F[Is],因此无需引入人工void。现在,这看起来更好:

**编辑**
JV(Jit& j,JV<T,N>* ptr, indices<Is...>) :
    jit(j), orig_ptr(ptr), F{{T(j,&ptr->F[Is])...}} { }

它可以编译并正常运行!

但是,第一个问题仍然存在:this 2x为什么相同?

最佳答案

模板参数包的扩展在这里:

F {{(void(Is),j,ptr->F[Is])...}}

非常有创造力,但不是您期望的。
(void(Is),j,ptr->F[Is])

是单个表达式,使用序列运算符(operator,)。其语义与以下“伪函数”块大致相同:
{
    (void)Is;
    (void)j;
    return ptr->F[Is];
}

请注意,Isj除了副作用之外没有其他作用。所以整个表达式等于
F {{(ptr->F[Is])...}}

老实说,我没有掌握您的代码的意图。这是我进行的一些概念验证,目的是验证之后您似乎使用的语法是否可以工作:
#include <iostream>
#include <vector>
#include <array>

typedef std::vector<std::string> Vec;

template <int... I>
    void foo(Vec const& v)
{
    std::array<std::string, sizeof...(I)> expand {{ v.at(I)... }};
    for(auto i: expand)
        std::cout << i << '\n';
}

int main()
{
    const Vec v { "zero", "one", "two", "three", "four", "etc" };
    foo<2,1,3,0>(v);
    foo<42>(v);
}

输出:
two
one
three
zero
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check

因此,它完全符合您的期望(在GCC和Clang++中进行了测试)

关于c++ - 与此指针相同,可变参数类型也有麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13383406/

10-15 14:39