I'm trying to write a templated operator== for a wrapper class around a std::variant. The idea is that the Setting class is comparible with other Setting objects, as well as the types supported by the variant. I have already solved this problem without templates as its easy to just write out the operators==, but its important for me to learn the templating way.
So, this is how I'd like the Setting to be used:
Setting s1("string");
Setting s2("string");
s1 == s2; // okay, equals true
As well as
Setting s3("string");
s3 == "string"; // should equal true
std::string s4 = "string";
s3 == s4; // also True
Here's what I've got so far, though I'm pretty sure I'm a long way off. The strategy is to template the operator= such that if the template argument T is a valid variant (setting_t) type, then extract the value from the variant as a type T and perform the comparison (T compares with T). Alternatively, when T is another Setting, we can just directly compare the setting_ member variables (Setting compares with Setting).
#include <type_traits>
#include <variant>
using setting_t = std::variant<std::string, int, double>;
/**
* Utility which is true when
* type T is in a variant, false otherwise.
* For instance,
* std::string x("a String");
* bool truth = isValidVariantType<decltype(x), setting_t>(); // true
*
* unsigned long x = 4;
* bool truth = isValidVariantType<decltype(x), setting_t>(); // false
*/
template<typename T, typename ALL_T>
struct isValidVariantType;
template<typename T, typename... ALL_T>
struct isValidVariantType<T, std::variant<ALL_T...>>
: public std::disjunction<std::is_same<T, ALL_T>...> {
};
class Setting {
public:
explicit Setting(setting_t setting)
: setting_(std::move(setting)) {}
template <typename T,
class = typename std::enable_if<isValidVariantType<T, setting_t>::value>::type>
bool operator==(const T& setting){
T val = std::get<T>(setting);
return val == setting;
}
private:
setting_t setting_;
};
I've spent a good number of hours on this now so I'd appreciate any advice you can give me. Thanks, in advance!
edit - compiler errors
As requested, here is what the compiler currently generates
When I run SettingTests.SettingVsSetting
TEST(SettingTests, SettingVsSetting){
Setting setting1("a String");
Setting setting2("a String");
// bool truth = setting1 == setting2;
}
generates the following compiler messages:
/home/ciaran/SettingTests/SRC/TemplateTutorialTests.cpp: In member function ‘virtual void SettingTests_SettingVsSetting_Test::TestBody()’:
/home/ciaran/SettingTests/SRC/TemplateTutorialTests.cpp:11:27: error: no match for ‘operator==’ (operand types are ‘Setting’ and ‘Setting’)
11 | bool truth = setting1 == setting2;
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Setting Setting
In file included from /home/ciaran/SettingTests/SRC/TemplateTutorialTests.cpp:2:
/home/ciaran/SettingTests/SRC/TermplateTutorial.hpp:32:10: note: candidate: ‘template<class T, class> bool Setting::operator==(const T&)’
32 | bool operator==(const T& setting){
| ^~~~~~~~
/home/ciaran/SettingTests/SRC/TermplateTutorial.hpp:32:10: note: template argument deduction/substitution failed:
/home/ciaran/SettingTests/SRC/TermplateTutorial.hpp:31:13: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
31 | class = typename std::enable_if<isValidVariantType<T, setting_t>::value>::type>
| ^~~~~
whilst Setting.SettingVsString
TEST(SettingTests, SettingVsString){
Setting setting1("a String");
std::string setting2("a String");
bool truth = setting1 == setting2;
}
generates
/home/ciaran/SettingTests/SRC/TermplateTutorial.hpp: In instantiation of ‘bool Setting::operator==(const T&) [with T = std::__cxx11::basic_string<char>; <template-parameter-1-2> = void]’:
/home/ciaran/SettingTests/SRC/TemplateTutorialTests.cpp:17:30: required from here
/home/ciaran/SettingTests/SRC/TermplateTutorial.hpp:33:28: error: no matching function for call to ‘get<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(const std::__cxx11::basic_string<char>&)’
33 | T val = std::get<T>(setting);
| ~~~~~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/10/bits/unique_ptr.h:36,
from /usr/include/c++/10/memory:83,
from /home/ciaran/SettingTests/googletest/googletest/include/gtest/gtest.h:57,
from /home/ciaran/SettingTests/SRC/TemplateTutorialTests.cpp:1:
/usr/include/c++/10/utility:223:5: note: candidate: ‘template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(std::pair<_Tp1, _Tp2>&)’
223 | get(std::pair<_Tp1, _Tp2>& __in) noexcept
| ^~~
/usr/include/c++/10/utility:223:5: note: template argument deduction/substitution failed:
/usr/include/c++/10/utility:228:5: note: candidate: ‘template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(std::pair<_Tp1, _Tp2>&&)’
228 | get(std::pair<_Tp1, _Tp2>&& __in) noexcept
| ^~~
... (it goes on like this for a while)
Edit 3 - alternative operator==
template<typename T,
class = typename std::enable_if<isValidVariantType<T, setting_t>::value>::type>
bool operator==(const T &setting) {
if (auto val = std::get_if<T>(&setting_)){
return *val == setting;
};
return false;
}