Я использую шаблонное выражение для пользовательского класса и не знаю, как заставить работать оператор+.
Вот минимальный код, воспроизводящий проблему: р>
D:\programmation\cpp\TestTemplateExpression\main.cpp: In instantiation of 'decltype(auto) operator+(const IntExpr&, const S&) [with T = IntAdd; S = int; std::enable_if_t* = 0]':
D:\programmation\cpp\TestTemplateExpression\main.cpp:96:38: required from here
D:\programmation\cpp\TestTemplateExpression\main.cpp:84:18: error: no matching function for call to 'IntAdd::IntAdd(const int&)'
84 | return lhs + T(rhs);
| ^~~~~~
D:\programmation\cpp\TestTemplateExpression\main.cpp:34:9: note: candidate: 'IntAdd::IntAdd(const E1&, const E2&) [with E1 = Int; E2 = Int]'
34 | IntAdd(E1 const& u, E2 const& v) : u_(u), v_(v)
| ^~~~~~
D:\programmation\cpp\TestTemplateExpression\main.cpp:34:9: note: candidate expects 2 arguments, 1 provided
D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: candidate: 'constexpr IntAdd::IntAdd(const IntAdd&)'
26 | class IntAdd : public IntExpr
| ^~~~~~
D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: no known conversion for argument 1 from 'const int' to 'const IntAdd&'
D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: candidate: 'constexpr IntAdd::IntAdd(IntAdd&&)'
D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: no known conversion for argument 1 from 'const int' to 'IntAdd&&'
< /code>
Я попытался использовать Typedef (комментированные строки), но определение класса не завершено в данный момент. < /p>
:\programmation\cpp\TestTemplateExpression\main.cpp: In instantiation of 'class IntExpr':
D:\programmation\cpp\TestTemplateExpression\main.cpp:41:7: required from 'class Int'
D:\programmation\cpp\TestTemplateExpression\main.cpp:96:29: required from here
D:\programmation\cpp\TestTemplateExpression\main.cpp:7:34: error: invalid use of incomplete type 'class Int'
7 | typedef typename E::Type Type; //KO
| ^~~~
D:\programmation\cpp\TestTemplateExpression\main.cpp:41:7: note: declaration of 'class Int'
41 | class Int : public IntExpr
| ^~~
Я использую шаблонное выражение для пользовательского класса и не знаю, как заставить работать оператор+. Вот минимальный код, воспроизводящий проблему: р> [code]#include
template class IntExpr { public: //typedef typename E::Type Type; //KO
static constexpr bool is_leaf = false;
template T number() const { return static_cast(*this).number(); }
return 0; } [/code] Полная ошибка: [code]D:\programmation\cpp\TestTemplateExpression\main.cpp: In instantiation of 'decltype(auto) operator+(const IntExpr&, const S&) [with T = IntAdd; S = int; std::enable_if_t* = 0]': D:\programmation\cpp\TestTemplateExpression\main.cpp:96:38: required from here D:\programmation\cpp\TestTemplateExpression\main.cpp:84:18: error: no matching function for call to 'IntAdd::IntAdd(const int&)' 84 | return lhs + T(rhs); | ^~~~~~ D:\programmation\cpp\TestTemplateExpression\main.cpp:34:9: note: candidate: 'IntAdd::IntAdd(const E1&, const E2&) [with E1 = Int; E2 = Int]' 34 | IntAdd(E1 const& u, E2 const& v) : u_(u), v_(v) | ^~~~~~ D:\programmation\cpp\TestTemplateExpression\main.cpp:34:9: note: candidate expects 2 arguments, 1 provided D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: candidate: 'constexpr IntAdd::IntAdd(const IntAdd&)' 26 | class IntAdd : public IntExpr | ^~~~~~ D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: no known conversion for argument 1 from 'const int' to 'const IntAdd&' D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: candidate: 'constexpr IntAdd::IntAdd(IntAdd&&)' D:\programmation\cpp\TestTemplateExpression\main.cpp:26:7: note: no known conversion for argument 1 from 'const int' to 'IntAdd&&' < /code> Я попытался использовать Typedef (комментированные строки), но определение класса не завершено в данный момент. < /p> :\programmation\cpp\TestTemplateExpression\main.cpp: In instantiation of 'class IntExpr': D:\programmation\cpp\TestTemplateExpression\main.cpp:41:7: required from 'class Int' D:\programmation\cpp\TestTemplateExpression\main.cpp:96:29: required from here D:\programmation\cpp\TestTemplateExpression\main.cpp:7:34: error: invalid use of incomplete type 'class Int' 7 | typedef typename E::Type Type; //KO | ^~~~ D:\programmation\cpp\TestTemplateExpression\main.cpp:41:7: note: declaration of 'class Int' 41 | class Int : public IntExpr | ^~~ [/code] Есть ли хитрость, чтобы решить эту проблему?