fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. template<typename T, size_t N>
  6. class Data
  7. {
  8. private:
  9. T data[N];
  10.  
  11. public:
  12. // default constructor
  13. Data() : data() {}
  14.  
  15. // template constructor from source
  16. template<size_t M>
  17. Data(const Data<T,M>& arg) : data()
  18. {
  19. // ERROR: can't access Data<T,M>::data
  20. std::copy_n(std::begin(arg.data), std::min(N,M), std::begin(data));
  21. }
  22.  
  23. template<size_t M>
  24. Data& operator =(const Data<T,M>& arg)
  25. {
  26. // same issue
  27. std::copy_n(arg.data, std::min(N,M), data);
  28. return *this;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34. Data<char,10> x1;
  35. Data<char,12> x2 = x1;
  36. return 0;
  37. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:35:19: warning: variable ‘x2’ set but not used [-Wunused-but-set-variable]
prog.cpp: In instantiation of ‘Data<T, N>::Data(const Data<T, M>&) [with unsigned int M = 10u; T = char; unsigned int N = 12u]’:
prog.cpp:35:24:   required from here
prog.cpp:9:13: error: ‘char Data<char, 10u>::data [10]’ is private
prog.cpp:20:9: error: within this context
stdout
Standard output is empty