このコードは失敗し、エラー メッセージが表示されます (行番号がオフになっています)。これを修正するにはどうすればよいですか (同じ意図を維持します)。
g++ -o c_test c_test.cpp
c_test.cpp: 関数 'int main(int, char**)' 内:
c_test.cpp:28:18: エラー: 'wcalc(CWrapped<5>::u_type&)' の呼び出しに一致する関数がありません
c_test.cpp:28:18: 注: 候補は:
c_test.cpp:17:58: 注: テンプレート int wcalc(typename CWrapped::u_type)
ラップされた型は "calc" 関数と "wcalc" 関数の両方に渡されますが、2 つ目の関数は失敗します。型をラップできるようにしたいので、コンパイル時の定義を使用してさまざまな型を指定できますが、同じラップされた関数を使用できます
// Example template class
template <int T_B>
class m_int {
public:
int x;
m_int() { x = T_B; }
int to_int() { return(x); }
};
// Desired Typedef wrap
template <int T_BITS> struct CWrapped {
typedef m_int<T_BITS> u_type;
};
// This is ok, no wrapping
template <int T_BITS> int calc(m_int<T_BITS> x) {
return(x.to_int());
}
// This fails when instantiated
template <int T> int wcalc(typename CWrapped<T>::u_type x) {
return(x.to_int());
}
int main(int argc, char* argv[]) {
CWrapped<5>::u_type s;
int x = calc(s);
int y = wcalc(s);
return(0);
}