我正在试图弄清楚如何将类型转换操作符添加到以下嵌套类以允许main编译,但我无法确定需要什么。在main中的最后一个赋值的旁边是导致问题。请注意,最后一个赋值使用类型转换来使其工作。我怀疑我需要为'add'类定义一个类型转换操作符,但是如何定义?。
很抱歉长篇名单,但这很简单,我知道如何制作它。
#include <iostream>
using namespace std;
template <char I> struct index { };
template <char I, char J> struct marker;
// simple class with just a 2 element int array
struct A {
A() { }
A(int i, int j) { a[0] = i; a[1] = j; }
A(const A& x) { a[0] = x(0); a[1] = x(1); }
template<char I, char J>
marker<I,J> operator()(const index<I>&, const index<J>&) {
return marker<I,J>(*this);
}
friend std::ostream& operator<<(std::ostream& os, const A& _A) {
return os << '{' << _A.a[0] << ',' << _A.a[1] << '}';
}
int& operator()(int i) { return a[i]; }
const int& operator()(int i) const { return a[i]; }
private:
int a[2];
};
template <char I, char J>
struct marker {
const int DI;
const int DJ;
marker(A& a) : _A(a), DI(1), DJ(0) { }
marker(A& a, const int i, const int j) : _A(a), DI(i), DJ(j) { }
marker(const marker& m) : _A(m._A), DI(m.DI), DJ(m.DJ) { }
// cast I,J => J,I
operator marker<J,I>() const {
return marker<J,I>(_A, DJ, DI);
}
marker& operator=(const marker& m) {
_A(0) = m(0);
_A(1) = m(1);
return *this;
}
// returns the i'th or (1-i)'th element of _A
int operator()(int i) const {
return _A(i*DI + (1-i)*DJ);
}
template<class LHS, class RHS>
struct add {
const LHS& lhs;
const RHS& rhs;
add(const LHS& l, const RHS& r) : lhs(l), rhs(r) { }
int operator()(int i) const {
return lhs(i) + rhs(i);
}
add< add,marker > operator+(const marker& b) {
return add< add,marker >(*this, b);
}
};
add< marker,marker > operator+(const marker& b) const {
return add< marker,marker >(*this,b);
}
template<class LHS>
void operator=(const add<LHS,marker>& expr) {
_A(0) = expr(0);
_A(1) = expr(1);
}
private:
A& _A;
};
int main() {
index<'i'> i;
index<'j'> j;
A a(1,2), b;
b(i,j) = a(j,i);
cout << b << endl; // "{2,1}"
b(i,j) = a(i,j) + a(j,i);
cout << b << endl; // "{3,3}"
b(i,j) = a(j,i) + a(i,j); // fails to compile
cout << b << endl; // should be "3,3"
b(i,j) = (marker<'i','j'>)a(j,i) + a(i,j); // works fine
cout << b << endl; // "{3,3}"
return 0;
}
答案 0 :(得分:1)
clang会出现以下错误:
/tmp/webcompile/_13759_1.cc:97:10: error: no viable overloaded '='
b(i,j) = a(j,i) + a(i,j); // fails to compile
~~~~~~ ^ ~~~~~~~~~~~~~~~
/tmp/webcompile/_13759_1.cc:44:11: note: candidate function not viable: no known conversion from 'add<marker<'j', 'i'>, marker<'j', 'i'> >' to 'const marker<'i', 'j'>' for 1st argument;
marker& operator=(const marker& m) {
^
/tmp/webcompile/_13759_1.cc:76:8: note: candidate template ignored: failed template argument deduction
void operator=(const add<LHS,marker>& expr) {
^
错误只是意味着编译器无法找到执行相关赋值的方法。然后编译器列出它尝试的两个operator=
。第一个显然无法匹配。对于第二个,failed template argument deduction
只意味着编译器无法弄清楚如何将参数转换为任何类型const add<LHS,marker>&
的{{1}}。也许您想要以下内容?
LHS
修改强> 如果显式调用operator =并强制模板参数,则问题变得更加明显:
template<class LHS, class RHS>
void operator=(const add<LHS,RHS>& expr)
具体而言,参数为/tmp/webcompile/_28787_0.cc:95:10: error: no matching member function for call to 'operator='
b(i,j).operator=<marker<'j', 'i'>, marker<'j', 'i'> >(a(j,i) + a(i,j)); // fails to compile
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/webcompile/_28787_0.cc:76:6: note: candidate function [with LHS = marker<'j', 'i'>, RHS = marker<'j', 'i'>] not viable: no known conversion from 'add<marker<'j', 'i'>, marker<'j', 'i'> >' (aka 'marker<'j', 'i'>::add<marker<'j', 'i'>, marker<'j', 'i'> >') to 'add<marker<'j', 'i'>, marker<'j', 'i'> >' (aka 'marker<'i', 'j'>::add<marker<'j', 'i'>, marker<'j', 'i'> >') for 1st argument;
void operator=(add<LHS,RHS>);
^
,参数为marker<'i', 'j'>::add
。
答案 1 :(得分:0)
template <class LHS>
void operator=(const typename marker<J,I>::template add< LHS, marker<J,I> >& expr) {
myA(0) = expr(1);
myA(1) = expr(0);
}
参数规范的语法让我感到困惑。 servn给了我线索,一些试验和错误使语法正确。