Category : templates

I have a templated class, whose type is to be determined by another sub templated constructor. template <typename V, typename I> class Text{ public: template <typename Container, typename V = typename Container::value_type, typename I = typename Container::size_type> Text(Container& c) {} }; So usage would be something like: std::vector<int> v; Text(v) //Deduces to Text<int, std::size_t> Unfortunately, ..

Read more

template <bool B, typename T = void> struct enable_if {using type = T;}; template <typename T> struct enable_if<false,T> {}; template<bool B, class T = void> using enable_if_t = typename enable_if<B,T>::type; template< typename T, enable_if_t<is_integral<T>::value,bool> = true > struct MyTypeWhichIsdependentOnIntegralTypes{}; The above code in C++ uses an enable_if, just wanted to know this weird syntax while ..

Read more

#include <type_traits> #include <iostream> template<typename T> struct Wrapper { T value; operator T&() & { std::cout << "call const ref" << std::endl; return this->value; } operator const T&() const& { std::cout << "call const ref" << std::endl; return this->value; } operator T&&() && { std::cout << "call move" << std::endl; return std::move(this->value); } operator const ..

Read more

how would you make the two lines generic without using std::any? Thanks. struct A { struct B { struct C { uint32_t ID = 0; uint32_t Mode = 0; // <<<<<<——– make generic uint32_t Value = 0; // <<<<<<——– make generic }; std::vector<C> moves; }; std::queue<B> PreparedMoves; }; I am open for any suggestions. Thanks ..

Read more

I’ve been facing a problem today. As the title said, calling a namespace function inside a template class function throw me a no member named ‘Windows’ in namespace ‘Editor’ Why and how can I solve this? Thanks in advance! This is the implementation of the template class function in a tpp #include "ImGuiFuncs/ImGuiWindows.hpp" template <typename ..

Read more

I have a contrived example of what I would like to achieve. template <int Val> struct Value { static constexpr auto aValue = Val; void arbitrary_operation() {}; }; struct Meta { }; static constexpr auto Values = std::tuple<Value<5>, Value<6>, Value<7>>; static constexpr auto ValueMetadata = std::array<std::pair<int, Meta>>{{{5, Meta}, {6, Meta}, {7, Meta}}}; … void do_work() ..

Read more

Pardon the confusing title. I have this code, which is accepted by GCC, Clang, and MSVC: #include <type_traits> template <typename T> struct Reader { friend auto adl(Reader<T>); }; template <typename T, typename U> struct Writer { friend auto adl(Reader<T>) {return U{};} }; struct A { struct Tag {}; auto helper() -> decltype(void(Writer<Tag,decltype(this)>{})) {} using Self ..

Read more

I’ve written a template function whose template arguments are being correctly deduced by GCC/Clang but are ambiguous to MSVC. Here is a compiler explorer link: https://godbolt.org/z/8b376EYYr The template is declared as: template <typename Int = int, template <typename…> typename Cont = std::vector> Cont<Int> input_to_int(Cont<std::string> input); and it is used in main(): std::vector<int> int_vec = input_to_int(str_vec); ..

Read more