same template for both regular pointer and shared_ptr - c++11

I have a template in C++ like below
template <class TYPE>
bool writeRecordForSet(std::vector<TYPE*> entityPtr){
if(entityPtr.size()== 0) return true;
...
}
I want to use the same template for std::shared_ptr; i.e. I have std::vector<std::shared_ptr>;
How can I use the same template?

I may have misunderstood the question but can't you just remove the * from the tempaltised function? Something like:
class Base {};
template <class Type>
bool writeRecordForSet(std::vector<Type> entityPtr){
if(entityPtr.size()== 0) return true;
//...
}
int main() {
std::vector<std::shared_ptr<Base>> vec_shared;
std::vector<int*> vec_intp;
std::vector<std::unique_ptr<Base>> vec_unique_ptr;
writeRecordForSet(vec_shared);
writeRecordForSet(vec_intp);
writeRecordForSet(vec_unique_ptr);
}
This way you could use both vector<type*> as well as vector<shared_ptr> as arguments to the function. (Or any other pointer type like unique_ptr)

This one does not work
class Base {
public:
int getVal() { return 0; }
};
template <class Type>
bool writeRecordForSet(std::vector<Type> entityPtr) {
if (entityPtr.size() == 0) return true;
//...
for (auto iter = entityPtr.begin(); iter != entityPtr.end(); iter++) {
Type enPtr = *iter;
int myval = enPtr->getVal();
}
return true;
}
int main() {
std::vector<std::shared_ptr<Base>> vec_shared;
std::vector<int*> vec_intp;
std::vector<std::unique_ptr<Base>> vec_unique_ptr;
writeRecordForSet(vec_shared);
writeRecordForSet(vec_intp);
writeRecordForSet(vec_unique_ptr);
}

Related

Why do std::arrays break templated functions with type checks?

I have a template function which performs certain actions depending on the type provided:
template<typename T>
T read()
{
if (std::is_integral<T>()) {
return static_cast<T>(std::stoi(readToken()));
}
else if (std::is_same<T, float>()) {
return std::stof(readToken());
}
else if (std::is_same<T, std::array<float, 3>>()) {
return { read<float>, read<float>, read<float> };
}
else throw std::logic_error("Invalid type");
}
The compiler gives the following warning when read<int>() is called:
control reaches end of non-void function
read is template. Every time this template is instantiated the whole body of the function is generated by compiler. When you call read<int>() section with array is also compiled, but it is not possible to assign array to return type which is int. That is why you get the error.
Under g++7.3 you can use if constexpr construction. With this, lines within a scope the if condition is true are only compiled:
template<typename T>
T read() {
if constexpr (std::is_integral<T>()) {
return static_cast<T>(std::stoi(readToken()));
}
else if constexpr (std::is_same<T, float>()) {
return std::stof(readToken());
}
else if constexpr (std::is_same<T, std::array<float, 3>>()) {
std::array<float, 3> arr;
arr.at(0) = 4; arr.at(1) = 2; arr.at(2) = 0;
return arr;
}
else throw std::logic_error("Invalid type");
}
Prior to c++17, you call helper function which is overloaded for all types you want to process:
template<class T>
T readHelper() {
throw std::runtime_error("invalid type");
}
// here provide types you want to be handled
template<>
int readHelper<int>() { return std::stoi(readToken()); }
template<>
float readHelper<float>() { return std::stof(readToken()); }
template<>
std::array<float,3> readHelper<std::array<float,3>>() {
return std::array<float,3>{};
}
template<class T>
T read2(){
return readHelper<T>();
}
Demo

How to get a reference to the negation of a bool?

For example, if I have a bool value v, I want a reference to !v that can change when v changes. An example use will be:
class A {
bool& isOpen;
A(bool& value): isOpen(value) {}
void f() {
if (isOpen) {
doSomething();
}
}
};
class B {
bool& isClosed;
B(bool& value): isClosed(value) {}
void g() {
if (isClosed) {
doSomething();
}
}
};
int main() {
bool isOpen = true;
A a(isOpen);
B b(negattive_reference_of(isOpen));
a.f(); // doSomething()
b.g(); // do nothing
isOpen = false;
a.f(); // do nothing
b.g(); // doSomething()
}
Is there anyway in C++ to acheive a similar effect?
Under the hood reference is equivalent to a constant pointer to some variable (compiler just gives you a syntax sugar of how to work with such pointers so that they are always initialized).
So you wan't to have the same variable and two different pointers to it, one of which will dereference to true and the other to false. That is obviously impossible.
The OOP -way to do it would be to pass not reference to boolean but some interface to your classes and use implementation that uses same boolean variable:
class IIsOpenProvider
{
public:
virtual ~IIsOpenProvider() = 0;
virtual bool GetOpenValue() = 0;
};
class IIsClosedProvider
{
public:
virtual ~IIsClosedProvider() = 0;
virtual bool GetClosedValue() = 0;
};
class ValueProvider : public IIsOpenProvider, public IIsClosedProvider
{
public:
bool GetOpenValue() override { return isOpen; }
bool GetClosedValue() override { return !isOpen; }
private:
bool isOpen;
};
class A {
IIsOpenProvider& isOpen;
A(IIsOpenProvider& value): isOpen(value) {}
void f() {
if (isOpen.GetOpenValue()) {
doSomething();
}
}
};
class B {
IIsClosedProvider& isClosed;
B(IIsClosedProvider& value): isClosed(value) {}
void g() {
if (IIsClosedProvider.GetClosedValue()) {
doSomething();
}
}
};
// usage
ValueProvider val;
A a(val);
B b(val);

Can a method of an class (in a shared_ptr) be tied to a static function in a traits class?

Historically, I've been using trait classes to hold information and apply that into a "generic" function that runs the same "algorithm." Only differed by the trait class. For example: https://onlinegdb.com/ryUo7WRmN
enum selector { SELECTOR1, SELECTOR2, SELECTOR3, };
// declaration
template < selector T> struct example_trait;
template<> struct example_trait<SELECTOR1> {
static constexpr size_t member_var = 3;
static size_t do_something() { return 0; }
};
template<> struct example_trait<SELECTOR2> {
static constexpr size_t member_var = 5;
static size_t do_something() { return 0; }
};
// pretend this is doing something useful but common
template < selector T, typename TT = example_trait<T> >
void function() {
std::cout << TT::member_var << std::endl;
std::cout << TT::do_something() << std::endl;
}
int main()
{
function<SELECTOR1>();
function<SELECTOR2>();
return 0;
}
I'm not sure how to create "generic" algorithms this when dealing with polymorphic classes.
For example: https://onlinegdb.com/S1hFLGC7V
Below I have created an inherited class hierarchy. In this example I have a base catch-all example that defaults all the parameters to something (0 in this case). And then each derived class sets overrides specific methods.
#include <iostream>
#include <memory>
#include <type_traits>
#include <assert.h>
using namespace std;
struct Base {
virtual int get_thing_one() {
return 0;
}
virtual int get_thing_two() {
return 0;
}
virtual int get_thing_three() {
return 0;
}
virtual int get_thing_four() {
return 0;
}
};
struct A : public Base {
virtual int get_thing_one() override {
return 1;
}
virtual int get_thing_three() override {
return 3;
}
};
struct B : public Base {
virtual int get_thing_one() override {
return 2;
}
virtual int get_thing_four() override{
return 4;
}
};
Here I created a simple factory, not elegant but for illustrative purposes
// example simple factory
std::shared_ptr<Base> get_class(const int input) {
switch(input)
{
case 0:
return std::shared_ptr<Base>(std::make_shared<A>());
break;
case 1:
return std::shared_ptr<Base>(std::make_shared<B>());
break;
default:
assert(false);
break;
}
}
So this is the class of interest. It is a class does "something" with the data from the classes above. The methods below are a simple addition example but imagine a more complicated algorithm that is very similar for every method.
// class that uses the shared_ptr
class setter {
private:
std::shared_ptr<Base> l_ptr;
public:
setter(const std::shared_ptr<Base>& input):l_ptr(input)
{}
int get_thing_a()
{
return l_ptr->get_thing_one() + l_ptr->get_thing_two();
}
int get_thing_b()
{
return l_ptr->get_thing_three() + l_ptr->get_thing_four();
}
};
int main()
{
constexpr int select = 0;
std::shared_ptr<Base> example = get_class(select);
setter l_setter(example);
std::cout << l_setter.get_thing_a() << std::endl;
std::cout << l_setter.get_thing_b() << std::endl;
return 0;
}
How can I make the "boilerplate" inside the setter class more generic? I can't use traits as I did in the example above because I can't tie static functions with an object. So is there a way to make the boilerplate example more common?
Somewhere along the lines of having a selector, say
enum thing_select { THINGA, THINGB, };
template < thing_select T >
struct thing_traits;
template <>
struct thing_traits<THINGA>
{
static int first_function() --> somehow tied to shared_ptr<Base> 'thing_one' method
static int second_function() --> somehow tied to shared_ptr<Base> 'thing_two' method
}
template <>
struct thing_traits<THINGB>
{
static int first_function() --> somehow tied to shared_ptr<Base> 'thing_three' method
static int second_function() --> somehow tied to shared_ptr<Base> 'thing_four' method
}
// generic function I'd like to create
template < thing_select T, typename TT = thing_traits<T> >
int perform_action(...)
{
return TT::first_function(..) + TT::second_function(..);
}
I ideally would like to modify the class above to something along the lines of
// Inside setter class further above
int get_thing_a()
{
return perform_action<THINGA>(...);
}
int get_thing_b()
{
return perform_action<THINGB>(...);
}
The answer is, maybe I can't, and I need to pass int the shared_ptr as a parameter and call the specific methods I need instead of trying to tie a shared_ptr method to a static function (in hindsight, that doesn't sound like a good idea...but I wanted to bounce my idea)
Whoever makes the actual call will need a reference of the object, one way or the other. Therefore, assuming you want perform_action to perform the actual call, you will have to pass the parameter.
Now, if you really want to store which function of Base to call as a static in thing_traits without passing a parameter, you can leverage pointer to member functions:
template <>
struct thing_traits<THINGA>
{
static constexpr int (Base::*first_function)() = &Base::get_thing_one;
...
}
template < thing_select T, typename TT = thing_traits<T>>
int perform_action(Base & b)
{
return (b.*TT::first_function)() + ...;
}
You can also play instead with returning a function object that does the call for you (and the inner function takes the parameter).
It all depends on who you need to make the call and what information/dependencies you assume you have available in each class/template.

using decltype w/ parameter pack for C++11

I'm trying to get the multi_index_t code from the second answer here answered by davidhigh to work with C++11. C++11 does not support auto& type returns.
I converted the return types for the class, but I don't understand how/if it's possible to support the helper function multi_index() without using C++14.
The code:
#include<array>
template<int dim>
struct multi_index_t
{
std::array<int, dim> size_array;
template<typename ... Args>
multi_index_t(Args&& ... args) : size_array(std::forward<Args>(args) ...) {}
struct iterator
{
struct sentinel_t {};
std::array<int, dim> index_array = {};
std::array<int, dim> const& size_array;
bool _end = false;
iterator(std::array<int, dim> const& size_array) : size_array(size_array) {}
iterator& operator++()
{
for (int i = 0;i < dim;++i)
{
if (index_array[i] < size_array[i] - 1)
{
++index_array[i];
for (int j = 0;j < i;++j) { index_array[j] = 0; }
return *this;
}
}
_end = true;
return *this;
}
std::array<int, dim>& operator*() { return index_array; }
bool operator!=(sentinel_t) const { return !_end; }
};
iterator begin() const { return iterator{ size_array }; }
iterator end() const { return typename iterator::sentinel_t{}; }
};
template<typename ... index_t>
auto multi_index(index_t&& ... index) // <-- this doesn't compile
{
static constexpr int size = sizeof ... (index_t);
auto ar = std::array<int, size>{std::forward<index_t>(index) ...};
return multi_index_t<size>(ar);
}
According to this answer, you can't recursively expand the variadic function template via decltype(). Any ideas?
C++11 does not support auto& type returns.
So you can simply explicit the types.
For multi_index() you have that return a multi_index_t<size>, where size is sizeof...(index_t), so you can write
template<typename ... index_t>
multi_index_t<sizeof...(index_t)> multi_index(index_t&& ... index)
According to this answer, you can't recursively expand the variadic function template via decltype.
Correct, but I don't see recursion in your multi_index() function, so I don't see how apply recursion over decltype().
If you really want (but why?), you can explicit the returning type through decltype() as follows
template<typename ... index_t>
auto multi_index(index_t&& ... index)
-> decltype( multi_index_t<sizeof...(index_t)>
{ std::array<int, sizeof...(index_t)>
{{ std::forward<index_t>(index) ... }} } )
but I don't see a reason to do this instead of simply explicit multi_index_t<sizeof...(index_t)>

Is it possible to have a branch of code conditionally be compiled in C++?

Title says it all really. Sample code which illustrates the spirit of the affaire:
if( std::is_constructible<T, unsigned long>::value )
{
unsigned long identity = collection.rbegin()->first + 1;
std::shared_ptr<T> newObject(new T(identity));
collection.insert( identity , newObject );
return true;
}
else
{
return false;
}
Tag dispatch.
template<class T>
bool foo_impl(std::true_type){
unsigned long identity = collection.rbegin()->first + 1;
std::shared_ptr<T> newObject(new T(identity));
collection.insert( identity , newObject );
return true;
}
template<class T>
bool foo_impl(std::false_type){
return false;
}
template<class T>
bool foo(){
return foo_impl<T>(std::is_constructible<T, unsigned long>());
}
Since the if statement can be determined at compile time, I'd expect the compiler to be smart and optimize it directly, just as if you had something like
if ( true ) {
// Some code
}
else {
// Anything here any decent compiler will ignore.
}
Another option is to wrap the behaviour you want in a function, and use std::enable_if:
template <typename T, typename = typename enable_if<is_constructible<T, int>::value>::type>
bool foo() {
return true;
}
template <typename T, typename = typename enable_if<!is_constructible<T, int>::value>::type>
bool foo() {
return false;
}
// ...
return foo<T>();
Example: http://ideone.com/sgNVr5
Yet another option is to specialize on the boolean value:
template <bool b> bool foo();
template <>
bool foo<true>(){
return true;
}
template <>
bool foo<false>() {
return false;
}

Resources