I am looking to create a subtraction operator to compare DayTime classes that are in a Template class (TemplateDay). I can’t create a generic operator that would allow these classes to be compared with each other.
TemplateDay :
namespace db
{
template <const char *__entity_alias, const char *__entity_name>
class TemplateDay : public IEntity, public IDay
{
public:
class ID;
class DayTime;
private:
static const std::string _table_name;
static const EntityAlias _entity_alias;
static const EntityName _entity_name;
std::shared_ptr<ID> _id;
std::shared_ptr<DayTime> _day_time;
public:
~TemplateDay() = default;
static const std::string &table_name();
static const IEntityAlias &alias();
static const EntityName &entity_name();
template <class Attribute>
inline TemplateDay &withID(Attribute &&id);
template <class Attribute>
inline TemplateDay &withDayTime(Attribute &&day_time);
void set_foreign_entity_by_alias(const IEntity &foreign_entity) override final;
void set_attribute_by_column_name(const IAttribute &attribute) override final;
const std::string &get_table_name() const override final;
virtual const IEntityAlias &get_alias() const override;
JsonObject toJson() const override final;
virtual std::unique_ptr<IEntity> clone() const override;
};
}
namespace db
{
inline extern constexpr const char DAY_ENTITY_ALIAS[] = "d";
inline extern constexpr const char DAY_ENTITY_NAME[] = "days";
typedef TemplateDay<DAY_ENTITY_ALIAS, DAY_ENTITY_NAME> Day;
inline extern constexpr const char DAY_BIRTHDAY_ENTITY_ALIAS[] = "bd";
inline extern constexpr const char DAY_BIRTHDAY_ENTITY_NAME[] = "birthday";
typedef TemplateDay<DAY_BIRTHDAY_ENTITY_ALIAS, DAY_BIRTHDAY_ENTITY_NAME> Birthday;
inline extern constexpr const char DAY_LAST_UPDATE_ENTITY_ALIAS[] = "lud";
inline extern constexpr const char DAY_LAST_UPDATE_ENTITY_NAME[] = "last_update";
typedef TemplateDay<DAY_LAST_UPDATE_ENTITY_ALIAS, DAY_LAST_UPDATE_ENTITY_NAME> LastUpdate;
inline extern constexpr const char DAY_CREATION_DAY_ENTITY_ALIAS[] = "cd";
inline extern constexpr const char DAY_CREATION_DAY_ENTITY_NAME[] = "creation_day";
typedef TemplateDay<DAY_CREATION_DAY_ENTITY_ALIAS, DAY_CREATION_DAY_ENTITY_NAME> CreationDay;
inline extern constexpr const char DAY_MODIFICATION_DAY_ENTITY_ALIAS[] = "md";
inline extern constexpr const char DAY_MODIFICATION_DAY_ENTITY_NAME[] = "modification_day";
typedef TemplateDay<DAY_MODIFICATION_DAY_ENTITY_ALIAS, DAY_MODIFICATION_DAY_ENTITY_NAME> ModificationDay;
inline extern constexpr const char DAY_SEARCH_DAY_ENTITY_ALIAS[] = "sd";
inline extern constexpr const char DAY_SEARCH_DAY_ENTITY_NAME[] = "search_day";
typedef TemplateDay<DAY_SEARCH_DAY_ENTITY_ALIAS, DAY_SEARCH_DAY_ENTITY_NAME> SearchDay;
inline extern constexpr const char DAY_LAST_PAID_DAY_ENTITY_ALIAS[] = "lpd";
inline extern constexpr const char DAY_LAST_PAID_DAY_ENTITY_NAME[] = "last_paid_day";
typedef TemplateDay<DAY_LAST_PAID_DAY_ENTITY_ALIAS, DAY_LAST_PAID_DAY_ENTITY_NAME> LastPaidDay;
inline extern constexpr const char DAY_ACCOUNT_OPENED_ENTITY_ALIAS[] = "dao";
inline extern constexpr const char DAY_ACCOUNT_OPENED_ENTITY_NAME[] = "day_account_opened";
typedef TemplateDay<DAY_ACCOUNT_OPENED_ENTITY_ALIAS, DAY_ACCOUNT_OPENED_ENTITY_NAME> DayAccountOpened;
inline extern constexpr const char DAY_SCORE_DAY_ENTITY_ALIAS[] = "sd2";
inline extern constexpr const char DAY_SCORE_DAY_ENTITY_NAME[] = "score_day";
typedef TemplateDay<DAY_SCORE_DAY_ENTITY_ALIAS, DAY_SCORE_DAY_ENTITY_NAME> ScoreDay;
}
}
Daytime :
template <const char *__entity_alias, const char *__entity_name>
class TemplateDay<__entity_alias, __entity_name>::DayTime : public AAttribute, public IUniqueAttribute
This is what I tried :
namespace db
{
template <const char *__entity_alias_rhs, const char *__entity_name_rhs,
const char *__entity_alias_lhs, const char *__entity_name_lhs>
int operator-(const typename TemplateDay<__entity_alias_rhs, __entity_name_rhs>::DayTime &lhs,
const typename TemplateDay<__entity_alias_lhs, __entity_name_lhs>::DayTime &rhs) noexcept
{
const std::string &str_lhs = lhs.toString();
const std::string &str_rhs = rhs.toString();
int year_s[2] = {(std::atoi(str_lhs.substr(0, 4).c_str()) - 1900) * 365,
(std::atoi(str_rhs.substr(0, 4).c_str()) - 1900) * 365};
int mon_s[2] = {std::atoi(str_lhs.substr(5, 2).c_str()) * 30,
std::atoi(str_rhs.substr(5, 2).c_str()) * 30};
int day_s[2] = {std::atoi(str_lhs.substr(8).c_str()),
std::atoi(str_rhs.substr(8).c_str())};
return (year_s[0] - year_s[1]) + (mon_s[0] - mon_s[1]) + (day_s[0] - day_s[1]);
}
}
And here is my compilation error:
error: no match for ‘operator-’ (operand types are ‘const db::TemplateDay<(& db::DAY_CREATION_DAY_ENTITY_ALIAS), (& db::DAY_CREATION_DAY_ENTITY_NAME)>::DayTime’ and ‘const db::TemplateDay<(& db::DAY_CREATION_DAY_ENTITY_ALIAS), (& db::DAY_CREATION_DAY_ENTITY_NAME)>::DayTime’)
152 | const int interval = current_day - creation_day;
| ~~~~~~~~~~~ ^ ~~~~~~~~~~~~
| | |
| | DayTime<[...],[...]>
| DayTime<[...],[...]>
note:candidate: ‘template<const char* __entity_alias_rhs, const char* __entity_name_rhs, const char* __entity_alias_lhs, const char* __entity_name_lhs> int db::operator-(const typename db::TemplateDay<__entity_alias_rhs, __entity_name_rhs>::DayTime&, const typename db::TemplateDay<__entity_alias_lhs, __entity_name_lhs>::DayTime&)’
157 | int operator-(const typename TemplateDay<__entity_alias_rhs, __entity_name_rhs>::DayTime &lhs,
note: template argument deduction/substitution failed:
note: couldn’t deduce template parameter ‘__entity_alias_rhs’
152 | const int interval = current_day - creation_day;
If anyone knows how to fix this, it would be greatly appreciated! I feel like I’ve tried everything.