Как заставить выражение выражения шаблона C ++?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Как заставить выражение выражения шаблона C ++?

Сообщение Anonymous »

Я использую выражение шаблона о пользовательском классе, и я не знаю, как заставить оператора работать. Следующий пример упрощается из исходного кода, который направлен на использование моего собственного целочисленного типа с шаблоном выражения, удерживая экземпляры Int в дереве шаблонов выражения. < /P>
Вот минимальный код, который воспроизводит проблему. : < /p>

Код: Выделить всё

#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();
}

template 
IntExpr& operator+=(T const& other)
{
*this = *this + other;

return *this;
}
};

template 
class IntAdd : public IntExpr
{
typename std::conditional::type u_;
typename std::conditional::type v_;

public:
static constexpr bool is_leaf = false;

IntAdd(E1 const& u, E2 const& v) : u_(u), v_(v)
{
}
};

template 
class Int : public IntExpr
{
public:
typedef T Type;

static constexpr bool is_leaf = true;

template 
Int(IntExpr const& expr)
{
number_ = expr.number();
}

explicit Int(int number) : number_{number}
{

}

T number() const
{
return number_;
}

Int& operator+=(Int const& other)
{
number_ += other.number_;

return *this;
}

private:
T number_;
};

template 
inline IntAdd operator+(IntExpr const& u, IntExpr const& v)
{
return IntAdd(*static_cast(&u), *static_cast(&v));
}
template * = nullptr>
inline decltype(auto) operator+(IntExpr const& lhs, S const& rhs)
{
//return lhs + Int(rhs); //KO
return lhs + T(rhs);
}

template * = nullptr>
inline decltype(auto) operator+(S const& lhs, IntExpr const& rhs)
{
//return rhs + Int(lhs); //KO
return rhs + T(lhs);
}

int main()
{
auto const a((Int(1) + 2) + 3);

return 0;
}
Полная ошибка (поскольку int не является экземпляром Int):

Код: Выделить всё

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&&'
Чтобы преобразовать int в экземпляр Int, я попытался использовать typedef (строки с комментариями), но определение класса на данный момент не завершено.
:\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>
Есть ли трюк, чтобы решить эту проблему? Может быть, использование std :: condental с рекурсией, если Intexpr на листьях, если это правда, но я не знаю, как.

Подробнее здесь: https://stackoverflow.com/questions/793 ... ssion-work
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»