C++ templates working with objects *and* pointers

I want to make a function template that will work with object instances and pointers as well. This is important to me as this is the first step towards implementing a function that operates on a vector of elements as well as a vector of pointers to elements. Here my code so far #include <functional>… Read More C++ templates working with objects *and* pointers

trouble exposing inherited constructors when using nested templating

Consider this simple class derived from std::variant: #include <string> #include <variant> class my_variant : public std::variant<int, std::string> { using variant::variant; public: std::string foo() { return "foo"; } }; Notice I am deliberately adding using variant:variant so that constructors from the base class are exposed in derived class. If you don’t do this, instances of my_variant… Read More trouble exposing inherited constructors when using nested templating

C++ – why can't the compiler deduce the types for the function template containing constructor parameters?

I want to use std::aligned_storage_t to decouple allocation and construction, so I write two little helpers to help me do this #include <type_traits> template <typename T> using Uninitialized = std::aligned_storage_t<sizeof(T), alignof(T)>; template <typename T, typename… Args> auto get_initialized(Uninitialized<T>& block, Args… args) -> T& requires( // If we allow type with default constructor, then it would… Read More C++ – why can't the compiler deduce the types for the function template containing constructor parameters?

returning an std::optional<Type>& reference where Type is an abstract class

Background: I have a City template class that contains a 2D array that is populated by types that conform to ISpecies abstract class. The grid can be any size. code: ISpecies class ISpecies { public: ISpecies() = default; virtual ~ISpecies() = default; virtual std::string toString() = 0; virtual void move() = 0; virtual void spawn()… Read More returning an std::optional<Type>& reference where Type is an abstract class

Why std::is_integral returns false for decltype(*t) where t is int*?

#include<iostream> using namespace std; int main() { int* t; using T = decltype(*t); cout << is_integral<T>::value << endl; return 0; } Why does the code above print 0? >Solution : *t is an lvalue expression, then decltype(*t) leads to a reference type as int&. b) if the value category of expression is lvalue, then decltype… Read More Why std::is_integral returns false for decltype(*t) where t is int*?