c++11 how to check template parameter pack has N args before calling function with N args - c++11

Following on from this extracting a template parameter pack with different types into a vector of doubles produces warnings and cigien's answer.
I have the following code:
enum class p_type {p1, p2, p3};
class testerx
{
public:
void process1(double a)
{
std::cout << "1" << a << std::endl;
};
void process2(double a, double b)
{
std::cout << "2" << a << " " << b << std::endl;
};
void process3(double a, double b, double c)
{
std::cout << "3" << a << " " << b << " " << c << std::endl;
};
};
// The template type
template<typename TESTER, typename... ARGS>
class tester_templatex
{
public:
explicit tester_templatex(p_type type) : m_type(type) {};
void process(ARGS... args)
{
// Create a vector to put the args into. use double since that can hold all of the types
// that I am using
size_t param_count = sizeof...(args);
std::cout << "PARAM COUNT X " << param_count << std::endl;
std::vector<double> args_vect = {static_cast<double>(args)...};
for (auto arg : args_vect)
{
std::cout << "arg: " << arg << std::endl;
}
// Now call the tester
std::cout << "running tester: ";
switch (m_type)
{
case p_type::p1:
if constexpr (sizeof...(args) == 1)
m_tester.process1(args...);
break;
case p_type::p2:
if constexpr (sizeof...(args) == 2)
m_tester.process2(args...);
break;
case p_type::p3:
if constexpr (sizeof...(args) == 3)
m_tester.process3(args...);
break;
}
std::cout << std::endl;
};
p_type m_type;
TESTER m_tester;
};
main:
int main() {
tester_templatex<testerx, int> templatex1(p_type::p1);
tester_templatex<testerx, int, double> templatex2(p_type::p2);
tester_templatex<testerx, int, double, int> templatex3(p_type::p3);
templatex1.process(4);
templatex2.process(4, 5.123);
templatex3.process(4, 5.123, 6);
return 0;
}
Here I have test class with 3 different functions. I have a template class which picks the function to call based on the p_type (bad name - dont ask!).
This works for c++17 compiled code. But I only have c++11 where I need to run this code. c++11 does not support if constexpr:
case p_type::p3:
if constexpr (sizeof...(args) == 3)
m_tester.process3(args...);
break;
Without the if constexpr I get errors that the m_tester.process1/2/3 functions that don't match the parameter pack because they don't have the right number of parameters.
How can I fix this for c++11? - is it possible with a similar method?
Is there another way to extract N arguments from a parameter pack in c++11? - or some sort of type traits check?

For each of your functions have an overload that does nothing:
template<typename... ARGS>
void process3(ARGS&...) { }
and then just call the function without testing for the size of the pack:
case p_type::p3:
m_tester.process3(args...);
break;
This should pick the non-templated function when there are suitably many arguments, and the function template in other cases.

Related

c++ std transform calls copy constructor multiple times

During implementation of a code copying of map elements into a vector I found out that std::transform calls the copy ctor of my custom class multiple times despite of prior vector size reservation. So I compared different ways to do this, and saw that the best method is a simple for-loop. So my question is: are these algorithms really more expensive? If so what is the advantage of them? Or does it not matter for small number of elements? Here my test code:
class A
{
public:
A() {}
A(int ai) : i(ai) {}
A(A& a) : i(a.i) { std::cout << "copy ctor " << i << std::endl; }
A(const A& a) : i(a.i) { std::cout << "copy const ctor " << i << std::endl; }
A(A&& a) : i(std::move(a.i)) { std::cout << "move ctor " << i << std::endl; }
// I removed overloaded operators because they are not called here...
private:
int i;
};
std::map<int, A> m;
m.insert(std::make_pair(0, A(7)));
m.insert(std::make_pair(1, A(3)));
m.insert(std::make_pair(2, A(1)));
std::vector<A> vec;
vec.reserve(m.size());
std::transform(m.begin(), m.end(), std::back_inserter(vec), [](const std::pair<int, A> & p) { return p.second; }); // 3 calls for each ctor
//... clean up and reserve vec
std::for_each(m.begin(), m.end(), [&vec](const std::pair<int, A> & p) { vec.push_back(p.second); }); // 2 calls for each ctor
//... clean up and reserve vec
for (const auto & p : m) // 1 call for each ctor
{
vec.emplace_back(p.second);
}
The element type in a std::map is std::pair<const Key, Value>.
In your example use use std::pair<int, A> without const, so you need to copy the pair. This adds a copy in the first two solutions. If you change it to std::pair<const int, A> you can see 2 constructor calls in the first iteration and only one in the second (on par with your for loop).
For the first iteration you now have 1 copy and one move. This is because your lambda needs to copy the A then back_inserter move constructs it into the vector. Normally I wouldn't worry about an extra move since they are supposed to be cheap.
However, you can get rid of it. We can force the lamba to not copy the A instance, by ,for example, specifying the return type to be a reference. So now the back_inserter will have to copy when it inserts, but that's it.
This version of your code does one copy per element for each solution:
std::map<int, A> m; m.insert(std::make_pair(0, A(7))); m.insert(std::make_pair(1, A(3))); m.insert(std::make_pair(2, A(1))); std::vector<A> vec;
std::cout << "Transform" << std::endl;
vec.reserve(m.size());
std::transform(m.begin(), m.end(), std::back_inserter(vec),
// changed the key type and return type.
[](const std::pair<const int, A> & p)->const A& { return p.second; });
std::cout << "for_each" << std::endl;
vec.resize(0);
vec.reserve(m.size());
std::for_each(m.begin(), m.end(),
// changed the key type.
[&vec](const std::pair<const int, A> & p) { vec.push_back(p.second); });
std::cout << "START\n";
vec.resize(0);
vec.reserve(m.size());
for (const auto & p : m) {
vec.emplace_back(p.second);
}

bad_lexical_cast Exception handling in c++

i am using lexical cast in a function for three different variables. Now if a bad_lexical_cast exception occurs i have to set default values respective to each variable. now how to find from which statement the exception is thrown?
You can assign the default values first and then wrap each boost::lexical_cast into a try-catch block.
Or, better, extract a function that does it for you:
#include <boost/lexical_cast.hpp>
#include <iostream>
template<class T, class S>
T lexical_cast_or_default(S s, T default_value) noexcept {
T value;
return boost::conversion::try_lexical_convert(s, value)
? value
: default_value
;
}
int main() {
double a = lexical_cast_or_default("abc", 3.14);
double b = lexical_cast_or_default("123", 3.14);
int c = lexical_cast_or_default<int>("456", 3.14);
std::cout << a << '\n';
std::cout << b << '\n';
std::cout << c << '\n';
}
Outputs:
3.14
123
456

What does String do that I'm not doing? c++11

I am still new to c++, so bear with me.
I was trying to learn more about how std::move works and I saw an example where they used std::move to move the string to a different function and then showed using std::cout that no string remained. I thought cool, let's see if I can make my own class and do the same:
#include <iostream>
#include <string>
class integer
{
private:
int *m_i;
public:
integer(int i=0) : m_i(new int{i})
{
std::cout << "Calling Constructor\n";
}
~integer()
{
if(m_i != nullptr) {
std::cout << "Deleting integer\n";
delete m_i;
m_i = nullptr;
}
}
integer(integer&& i) : m_i(nullptr) // move constructor
{
std::cout << "Move Constructor\n";
m_i = i.m_i;
i.m_i = nullptr;
}
integer(const integer& i) : m_i(new int) { // copy constructor
std::cout << "Copy Constructor\n";
*m_i = *(i.m_i);
}
//*
integer& operator=(integer&& i) { // move assignment
std::cout << "Move Assignment\n";
if(&i != this) {
delete m_i;
m_i = i.m_i;
i.m_i = nullptr;
}
return *this;
}
integer& operator=(const integer &i) { // copy assignment
std::cout << "Copy Assignment\n";
if(&i != this) {
m_i = new int;
*m_i = *(i.m_i);
}
return *this;
}
int& operator*() const { return *m_i; }
int* operator->() const { return m_i; }
bool empty() const noexcept {
if(m_i == nullptr) return true;
return false;
}
friend std::ostream& operator<<(std::ostream &out, const integer i) {
if(i.empty()) {
std::cout << "During overload, i is empty\n";
return out;
}
out << *(i.m_i);
return out;
}
};
void g(integer i) { std::cout << "G-wiz - "; std::cout << "The g value is " << i << '\n'; }
void g(std::string s) { std::cout << "The g value is " << s << '\n'; }
int main()
{
std::string s("Hello");
std::cout << "Now for string\n";
g(std::move(s));
if(s.empty()) std::cout << "s is empty\n";
g(s);
std::cout << "\nNow for integer\n";
integer i = 77;
if(!i.empty()) std::cout << "i is " << i << '\n';
else std::cout << "i is empty\n";
g(i);
std::cout << "Move it\n";
g(std::move(i)); // rvalue ref called
if(!i.empty()) std::cout << "i is " << i << '\n';
else std::cout << "i is empty\n";
g(i);
return 0;
}
And this is my output:
Now for string
The g value is Hello
s is empty
The g value is
Now for integer
Calling Constructor
Copy Constructor
i is 77
Deleting integer
Copy Constructor
G-wiz - Copy Constructor
The g value is 77
Deleting integer
Deleting integer
Move it
Move Constructor
G-wiz - Copy Constructor
The g value is 77
Deleting integer
Deleting integer
i is empty
Copy Constructor
Process returned 255 (0xFF) execution time : 7.633 s
Press any key to continue.
As you can see, it crashes when it enters g the second time, never even getting to the operator<<() function. How is it that the empty std::string s can be passed to g where my empty integer i crashes the program?
Edit: Fixed new int vs. new int[] error. Thanks n.m.
Your "empty integer" crashes the program because it contains a null pointer. You are trying to dereference it when you use it at the right hand side of the assignment.
An empty string is a normal usable string. There are no unchecked null pointer dereferences in the std::string code.
You have to ensure that the empty state of your object is a usable one. Start with defining a default constructor. Does it make sense for your class? If not, then move semantic probably doesn't either. If yes, a moved-from object in the move constructor should probably end up in the same state as a default-constructed object. A move assignment can act as a swap operation, so there the right-hand-side may end up either empty or not.
If you don't want to define a usable empty state for your class, and still want move semantics, you simply cannot use an object after it has been moved from. You still need to make sure that an empty object is destructible.

How to connect a boost signal for only once?

How do I achieve the Qt::UniqueConnection mechanism in Boost?
Basically, I only want to connect the signal for only once.
As far as I know, there's no such thing, but you can easily mimic it using a custom combiner.
1. Using the custom Combiner
You can just ignore any excess slots. This combiner, for example, just ignores anything beyond the first slot (that is, the first added with at_front and no group, or the first in the group with the highest priority, see Ordering Slot Call Groups):
template<typename Result>
struct call_first {
typedef boost::optional<Result> result_type;
template<typename It>
result_type operator()(It first, It last) const {
if (first != last)
return *first;
return boost::none;
}
};
Now here's a test case: Live On Coliru
#include <iostream>
#include <boost/optional/optional_io.hpp>
int foo(int i) { std::cout << "(foo)"; return i*2; }
int bar(int i) { std::cout << "(bar)"; return i+2; }
int main()
{
using namespace boost::signals2;
signal<int(int), call_first<int> > sig;
std::cout << sig(42) << "\n"; // should be none ("--")
sig.connect(foo);
std::cout << sig(42) << "\n"; // should return 42*2
sig.connect(bar);
std::cout << sig(42) << "\n"; // should still return 42*2
}
Printing
--
(foo) 84
(foo) 84
2. Fix your connection management
You can make it easy to avoid double-connecting a signal, by using pretty simple helpers (assuming c++11 for this sample):
template <typename Sig, typename F> void try_connect(Sig& sig, F&& f) {
if (!sig.num_slots()) sig.connect(std::forward<F>(f));
}
template <typename Sig, typename F> void re_connect(Sig& sig, F&& f) {
sig.disconnect_all_slots();
sig.connect(std::forward<F>(f));
}
The corresponding test case Live On Coliru:
int main()
{
using namespace boost::signals2;
signal<int(int)> sig;
std::cout << sig(42) << "\n"; // should be none ("--")
try_connect(sig, foo);
std::cout << sig(42) << "\n"; // should return 42*2
try_connect(sig, bar);
std::cout << sig(42) << "\n"; // should still return 42*2
re_connect(sig, bar);
std::cout << sig(42) << "\n"; // should return 42+2
}
Output:
--
(foo) 84
(foo) 84
(bar) 44

Trouble with Nested Lambdas

I am probably making some elementary mistake here but given:
std::array<int, 3> arr = { 1, 2, 3 };
std::vector<int> vecint;
vecint.push_back(1);
vecint.push_back(2);
This is one obvious way to compare the elements in arr with the ones in vecint.
std::for_each(vecint.begin(), vecint.end(), [&arr](int vecvalue) {
for (auto arritr = arr.begin(); arritr != arr.end(); ++arritr) {
if (vecvalue == *arritr) {
std::cout << "found!!! " << vecvalue << "\n";
}
}
});
However, should I be able to do it like this too?
std::for_each(vecint.begin(), vecint.end(), [&arr](int vecvalue) {
if (std::find(arr.begin(), arr.end(), [=](int arrval) { return vecvalue == arrval; }) != arr.end()) {
std::cout << "found!!! " << vecvalue << "\n";
}
});
The latter fails to compile in VC11 with the following error:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3186): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion)
What am I missing?
cppreference on std::find and std::find_if
std::find takes a value to compare with as the third parameter, whereas std::find_if takes a UnaryPredicate (a function object taking one parameter). You probably just had a typo / wanted to use std::find_if.
Using std::find_if works for me. Live example.
#include <array>
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
std::array<int, 3> arr = {{ 1, 2, 3 }};
std::vector<int> vecint;
vecint.push_back(1);
vecint.push_back(2);
std::for_each
(
vecint.begin(), vecint.end(),
[&arr](int vecvalue)
{
if (std::find_if(arr.begin(), arr.end(),
[=](int arrval) { return vecvalue == arrval; })
!= arr.end())
{
std::cout << "found!!! " << vecvalue << "\n";
}
}
);
}
A simpler version is of course to use std::find (correctly):
std::for_each
(
vecint.begin(), vecint.end(),
[&arr](int vecvalue)
{
if (std::find(arr.begin(), arr.end(), vecvalue) != arr.end())
{
std::cout << "found!!! " << vecvalue << "\n";
}
}
);
Then, there's of course the range-based-for-loop variant, if your compiler supports it:
for(auto const& ve : vecint)
{
for(auto const& ae : arr)
{
if(ve == ae)
{
std::cout << "found!!! " << ve << "\n";
}
}
}
If your ranges are sorted, there are faster algorithms to get the intersection. Either you write your own loop to invoke an action for each element in the intersection, or you let the Standard Library copy the intersection into a new container:
#include <iterator> // additionally
std::vector<int> result;
std::set_intersection(arr.begin(), arr.end(), vecint.begin(), vecint.end(),
std::back_inserter(result));
for(auto const& e : result)
{
std::cout << e << std::endl;
}
What happens under the hood - why you get that error:
std::find is defined as (from cppreference):
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );
That is, the type of value and the type of the iterators are independent. However, in the implementation of std::find, there has to be a comparison like:
if(*first == value) { return first; }
And at this point, you're comparing an int (type of the expression *first) with a lambda (type of value) more precisely: with a closure type. This is ill-formed (luckily), as there's no conversion from a lambda to an int, and there's no comparison operator declared that's applicable here.

Resources