Referencing list items in templated task list

I’m currently looking at how make a task list templated, that all works fine and shows up as an app in the Add Apps area, however in the Task List I want to template there are columns where the content is a reference to another list on the site.

enter image description here

Is there a way to template a collection of lists as part of one application and maintain those relations?

The task list would be created on the same tenant and would be a on a subsite.

How to automatically create a templated Google sheet?

I’m trying to figure out how to automatically create a templated Google sheets worksheet when a new entry is made in a different sheet.

So the flow would be:

  1. Person’s name is added into Google sheet A, along with data point X
  2. This automatically creates a new Google sheet B
  3. Google sheet B is already pre-populated from a template with a few data points. It is automatically also populated with Person’s name and data point X

I haven’t been able to find solutions that include the pre-population. Any examples I could draw on? Is Google add-ons a good approach here?

Templated division by a power of 2

I’ve implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases where I (the developer) know the divisor will be a power of 2, but I can’t “prove” this in code to the compiler.

template <typename P> constexpr P log2_of_power_of_2(P non_negative_power_of_2) noexcept {     static_assert(std::is_integral<P>::value, "Only integral types are supported");     static_assert(sizeof(P) <= sizeof(unsigned long long), "Unexpectedly large type");     using cast_target_type = typename         std::conditional<sizeof(P) <= sizeof(unsigned),             unsigned,             typename std::conditional<sizeof(P) <= sizeof(unsigned long),                 unsigned long,                  unsigned long long             >::type         >::type;     return log2_of_power_of_2<cast_target_type>(         static_cast<cast_target_type>(non_negative_power_of_2)); } template <>  constexpr unsigned  log2_of_power_of_2<unsigned>(unsigned non_negative_power_of_2) noexcept  { return __builtin_ctz  (non_negative_power_of_2); }  template <>  constexpr unsigned long  log2_of_power_of_2<unsigned long>(unsigned long non_negative_power_of_2)  noexcept { return __builtin_ctzl (non_negative_power_of_2); }  template <> constexpr unsigned long long  log2_of_power_of_2<unsigned long long>(unsigned long long non_negative_power_of_2) noexcept  { return __builtin_ctzll(non_negative_power_of_2); }  template <typename I, typename P> constexpr I div_by_power_of_2(I dividend, P power_of_2_divisor) noexcept { return dividend >> log2_of_power_of_2(power_of_2_divisor); } 

Questions:

  • Is my approach to covering the possible types of power_of_2 appropriate? Can it perhaps be made less verbose but with the same effect?
  • Am I reinventing the wheel with this code?
  • Currently, this depends on certain compiler intrinsics available in GCC and clang but not necessarily elsewhere. I could generalize it a bit using this method to also support MSVC. Is there a better approach to generalizing the code?
  • Should I change the return type of log2_of_power_of_2 functions to be uniform rather than I? e.g. an unsigned?
  • Any other comments/suggestions are welcome.

Notes:

  • This is intended to be C++11; obviously with C++17 I could simplify it further
  • The constexpr qualifier is not very meaningful, since in a constexpr context we could just do plain division, but I’ve tacked it on nonetheless. To make the use of these utility function(s) more uniform.

Cleanest way of making templated friend function with required template parameters visible to ADL

Given the following example:

template<typename T> struct foo {     template<typename O>     friend foo<O> cast(foo const &) { return {}; }     friend foo bar(foo const &) { return {}; } };  int main(int, char**) {     foo<int> a;     auto b = bar(a);     auto c = cast<double>(a);     return 0; } 

cast is not visible for argument dependent lookup (ADL), while bar is. This is due to the fact that cast is a templated friend function with a required template parameter. What is the cleanest way of making cast visible to ADL?

The shortest is probably

template<typename O> void cast(); 

which can be enhanced by adding delete to make it clear that it cannot be called at compile time instead of at link time:

template<typename O> void cast() = delete; 

Furthermore an arbitrary argument could be added as well, to make the expected number of parameters visible, such as

template<typename O> void cast(void *) = delete; 

However the type here does not match the actual final type. In this simple case, I could use

template<typename O, typename T> void cast(foo<T> const &) = delete; 

But if I later add another class, e.g., qux, which also provides a friend cast this might actually be more misleading. A template-template parameter, while possible seems overly complex.

Which version would you prefer, and why, or do you have better suggestions?

The actual code has a specialized cast function (with a longer, more descriptive name).

Storing inherited specialized templated types into a vector of shared pointers?

I’m working on a Register class that will be used in a virtual pc application. I’m trying to make them as basic and as fast as possible so they should be trivially default constructible and trivially default copyable at the least.

My register types will rely on typedefs for their types:

Register.h

#pragma once  #include <vector> #include <memory>  typedef std::uint8_t u8; typedef std::uint16_t u16; typedef std::uint32_t u32; typedef std::uint64_t u64;  struct Register {     virtual void* getValue() = 0; };  template<typename T> struct Register_ty : Register {     void* data_;     explicit Register_ty(T* data) : data_(data) {}      virtual void* getValue() = 0; };  template<typename T> struct Register_t : Register_ty<T> {};  template<> struct Register_t<u8> : Register_ty<u8> {     explicit Register_t(u8* data) : Register_ty<u8>( data ) {}     void* getValue() override {         return data_;     } };  template<> struct Register_t<u16> : Register_ty<u16> {     explicit Register_t(u16* data) : Register_ty<u16>(data) {}     void* getValue() override {         return data_;     } };  template<> struct Register_t<u32> : Register_ty<u32> {     explicit Register_t(u32* data) : Register_ty<u32>(data) {}     void* getValue() {         return data_;     } };  template<> struct Register_t<u64> : Register_ty<u64> {     explicit Register_t(u64* data) : Register_ty<u64>(data) {}     void* getValue() {         return data_;     } };  struct Memory {     std::vector<Register*> bank; // vector };  

And when I go to use it as such:

#include <iostream> #include "Register.h"  int main() { u8 a = 8; Register_t<u8> r8(&a);  u16 b = 16; Register_t<u16> r16(&b);  u32 c = 32; Register_t<u32> r32(&c);  u64 d = 64; Register_t<u64> r64(&d);  Memory mem; mem.bank.push_back( &r8 ); mem.bank.push_back( &r16 ); mem.bank.push_back( &r32 ); mem.bank.push_back( &r64 );  for (auto& b : mem.bank) {     std::cout << b->getValue() << '\n'; }      return EXIT_SUCCESS; } 

Everything at this point seems okay and it gives me a possible output of:

000000000029F6B4 000000000029F704 000000000029F754 000000000029F7A8 

Which is giving me the address of the pointers. However when I try to dereference them as such:

for ( auto& b : mem.bank ) {     std::cout << *b->getValue() << '\n'; } 

This fails to compile so I had to go back and rethink my code… I came up with this so far:

#include <iostream> #include "Register.h"  int main() {     using namespace vpc;      u8* pa = nullptr;     u8 a = 8;     pa = &a;     Register_t<u8> r8(pa);      u16* pb = nullptr;     u16 b = 16;     pb = &b;     Register_t<u16> r16(pb);      u32* pc = nullptr;     u32 c = 32;     pc = &c;     Register_t<u32> r32(pc);      u64* pd = nullptr;     u64 d = 64;     pd = &d;     Register_t<u64> r64(pd);      Memory mem;     mem.bank.push_back( &r8 );     mem.bank.push_back( &r16 );     mem.bank.push_back( &r32 );     mem.bank.push_back( &r64 );      for (auto& b : mem.bank) {         std::cout << b->getValue() << '\n';     }      std::cout << '\n';      for (auto& b : mem.bank) {         auto p = b->getValue();         auto res = reinterpret_cast<u8*>(p);          std::cout << static_cast<u16>(*res) << '\n';     }     return EXIT_SUCCESS; }; 

And it has given me the output of:

00000000001DF694 00000000001DF704 00000000001DF774 00000000001DF7E8  8 16 32 64 

Which is now giving me the correct values. This is the first time I was able to achieve this behavior and I had to use pointer manipulation to do it. Originally I wasn’t using pointers at all and I wasn’t using void* in my classes.

Then I couldn’t just create a variable and pass it by its address as that wasn’t working either so I had to create variables of those types in my main code and initialize them with something, I had to create pointers of those types and initialize them with nullptr, and finally I had to assign my pointers to their respective variable type addresses.

Then to be able to retrieve the actual values from these pointers by dereferencing them I had to do some pointer manipulation by casting then I had to cast that dereferenced type.

for (auto& b : mem.bank) {     // create an auto p and get the value; p should be a void*     auto p = b->getValue();      // create an auto type for our result and cast our     // void* p to a u8* {smallest type} and {char} based     auto res = reinterpret_cast<u8*>(p);      // now since the underlying type is char I have to deference my     // res* and static cast it to a u16.      std::cout << static_cast<u16>(*res) << '\n'; } 

This bit of hack isn’t very pretty or friendly. It works so far however I can already see some issues ahead of time. If I have a Register that is of a u64 type and it has a majority of its bits filled with data that is needed and I reinterpret its pointer to a u8*, dereference that and cast that to a u16 type I’m going to lose information, however this is the only combination that it provided me with the correct results.

About this code design or implementation:

  • Is there an easier and cleaner way to achieve this?
  • How significant would be the loss of data in this particular implementation?
  • Are there any other problems that you might see that I’ve over looked?

I’d like to know how I can go about doing this as I haven’t even tried to store these Register types as a shared_ptr in the vector yet… and all of this conversion and use of pointers is okay but looks ugly and prone to possible future problems.

Deep copy of an unknown(ish) type in a templated linked list

I have a templated linked list (Inventory), and I have instantiated a inventory object of type Item *

Inventory<Item*> myinv; 

Item is an abstract base class, I have two other classes, Weapon and Armor which both publicly derive from the Item ABC.

I have instantiated a Weapon object:

Weapon * frost = new Weapon("Weapon", 100, 0.01, 100); 

I have used my function PushFront(const T & data)

myinv.PushFront(frost); 

The function copies over the pointer to said data into the new nodes m_data, instead of doing a deep copy

template<class T> inline void Inventory<T>::PushFront(const T & data) {  Node<T> * nn = new Node<T>;      nn->m_data = data; //Copies over pointer instead of calling an overloaded assignment operator      m_count += 1; . . . 

I cannot for the life of me figure out the syntax to get it to use an overloaded assignment operator, I can post more code to those of you that need it.

Thank you for any help.

C++ Templated single value container (std::any) with meta data [on hold]

My intention is to implement a single value container which stores all possible types, like QVariant or others does. I tried to take advantage of std::any which gives a type safe container for the same purpose. Additionally I would like to store some meta data (timestamps mostly) which refer to target value.

I came up so far with two solutions which does not work properly. The class template version Value works but the interface does not fit properly in my opinion. See the first get function for an example. One need to know which type is returned on invocation. auto could help, but not in all situations.

The second solution has a pretty nice interface, like QVariant. See the access function get which returns LateValue. After calling this function one can dig deeper for the stored type and so on. Problem here is whenever I put this stuff into a library the template methods (getter and constructor) do not get instantiated. One can do this manually with the getter method but not with the constructor, it is simply not possible.

#include <any> #include <memory> #include <iostream>  class Time {     int m_a, m_b; public:     Time(int a, int b) : m_a(a), m_b(b) {}     int a() const { return m_a; }     int b() const { return m_b; }      friend std::ostream &operator<<(std::ostream &os, const Time &time) noexcept {         os << time.a() << ' ' << time.b();         return os;     } };  template<typename T> class Value {     struct Impl {         std::any value;     };     std::unique_ptr<Impl> m_p; public:     explicit Value(const T &value) : m_p(std::make_unique<Impl>()) {         m_p->value = std::make_any<T>(value);     }     T get() const {         return std::any_cast<T>(m_p->value);     } };  class LateValue {     struct Impl {         std::any value;     };     std::unique_ptr<Impl> m_p; public:     template<typename T>     LateValue(const T &value) : m_p(std::make_unique<Impl>()) {         m_p->value = std::make_any<T>(value);     }     template<typename T>     T get() const {         return std::any_cast<T>(m_p->value);     } }; 

Here is a simple test program. Note that the above code goes into the same file.

template<typename T> Value<T> get() {     return Value<T>(1.2f); }  LateValue get() {     return LateValue(1.2f); }  int main() {     Value<int>   v0(1);     Value<float> v1(1.234f);     Value<Time>  v2(Time(1, 2));      LateValue l0(1);     LateValue l1(1.234f);     LateValue l2(Time(1, 2));      std::cout << v0.get() << ' '               << v1.get() << ' '               << v2.get() << '\n'               << l0.get<int>() << ' '               << l1.get<float>() << ' '               << l2.get<Time>() << '\n';      auto value = get<float>();     std::cout << " value is " << value.get() << '\n';      // l2.get<double>(); <--- exception: bad_any_cast (ok to me)      return 0; } 

The output is simply as in code, because it is working (single file!):

1 1.234 1 2 1 1.234 1 2  value is 1.2 

So the question is: How to improve the implementation to have a nice interface with simplicity of template parameter? (I try to overcome the necessity to implement a get method for every supported type.)


To clearify: I want a single value container which holds any type which I can access later. I do not want to cast the contained type in any way (ok to me in comments). The Implementations Value and LateValue do literally the same but provide different interfaces. Value needs to know the contained type on definition (see first get function), LateValue needs the type on access. The latter interface is what I need but the provided implementation does not work when using from a library.