Const keyword with function signature - c++

Here is my simple code:
#include <iostream>
using namespace std;
class alloc { };
template <typename T, typename Alloc = alloc>
class vector
{
public:
void swap(vector<T,Alloc> &v) { cout << "swap()" << endl; }
};
template <typename T, typename Alloc>
void swap(const vector<T,Alloc> &v1,const vector<T,Alloc> &v2)
{
v1.swap(v2);
}
int main()
{
vector<int> x;
vector<int> y;
swap(x,y);
return 0;
}
The code snippet run without problem.But i can't get any outputs
Then I delete const keyword.
void swap(vector<T,Alloc> &v1,vector<T,Alloc> &v2)
I get output swap()
I have read "The reason is that const for the parameter only applies locally within the function, since it is working on a copy of the data. This means the function signature is really the same anyways."
So i thought there is no difference between write or not write const.if i insist to write const here,how do i modify the code to get the output swap()

This is an excellent illustration of why using std should be avoided.
In order to debug this problem, remove using std, and add std:: in places where you want the behavior from the standard library. Fortunately, there is only one such place, i.e. the swap function in the template class:
void swap(vector<T,Alloc> &v) { std::cout << "swap()" << std::endl; }
Now try compiling again to see the error preventing your swap with const from being used:
prog.cpp:19:5: error: passing const vector<int> as this argument discards qualifiers
When your program was using std C++ had an alternative of picking std::swap over your own swap function when your function was not applicable. That is exactly what it did, without any warning, because it assumed that it is what you want it to do.
The error also tells you what to do in order to make const-qualified vectors be accepted: add const to the parameters of vector::swap, like this:
void swap(const vector<T,Alloc> &v) const { std::cout << "swap()" << std::endl; }
Now your program compiles and runs again (demo).

Related

Compile-time detection of missing user-defined to_string()

I want to provide a to_string(obj) function for every object type I create.
I found this question, applied the accepted answer, and it works. So far so good.
Then I created a new type, but forgot to write a to_string() for it (or better: I accidentally made it unreachable by ADL). The problem is: my program still compiles fine, and at runtime I get an obscure stack overflow(TM).
Is there a way to obtain a reasonable error message, instead?
Here is a small program to demonstrate the problem: an infinite recursion between notstd::to_string() and notstd::adl_helper::as_string().
#include <iostream>
#include <string>
namespace notstd {
namespace adl_helper {
using std::to_string;
template<class T>
std::string as_string( T&& t ) {
return to_string( std::forward<T>(t) );
}
}
template<class T>
std::string to_string( T&& t ) {
std::cout << "called" << std::endl; // <-- this is to show what's going on
return adl_helper::as_string(std::forward<T>(t));
}
class A {
/* both versions are needed, or the perfect forwarding candidate will
* always be chosen by the compiler in case of a non-perfect match */
//friend std::string to_string(A &a) { return std::string("a"); }
//friend std::string to_string(const A &a) { return std::string("a"); }
};
}
int main(int argc, char** argv) {
notstd::A a;
std::cout << to_string(a) << std::endl;
}
I tried creating a wrapper function that accepts one more parameter, to be used to perform the an anti-recursion check, like this:
#include <iostream>
#include <string>
#include <cassert>
namespace notstd {
namespace wrap_std {
std::string to_string(double v, bool) { return std::to_string(v); }
/* .... etc..... */
}
namespace adl_helper {
using wrap_std::to_string;
template<class T>
std::string as_string( T&& t ) {
return to_string( std::forward<T>(t), true );
}
}
template<class T>
std::string to_string( T&& t, bool recurring = false ) {
std::cout << "called" << std::endl;
assert(!recurring);
return adl_helper::as_string(std::forward<T>(t));
}
class A {
/* both versions are needed, or the perfect forwarding candidate will
* always be chosen by the compiler in case of a non-perfect match */
//friend std::string to_string(A &a) { return std::string("A"); }
//friend std::string to_string(const A &a) { return std::string("A"); }
};
}
int main(int argc, char** argv) {
notstd::A a;
std::cout << to_string(a) << std::endl;
}
The problems here are:
I'd have to wrap all std::to_string() overloads
I'll only get a runtime error, but I feel the problem could and should be detected ad compile time
I'm probably adding some overhead, for something useful only during development: maybe I could add some macros to deactivate all this in release mode, but it would add even more work
Maybe I could use a template to wrap std::to_string() and create specializations for my types... this would be a quite different beast, but at least it would provide a compile time error if a suitable specialization is not available. I would have, again, to wrap all std::to_string() overloads, and I'd probably have to (almost) forget about ADL, at least until c++20 is supported by all compilers, If I understand well.
Does anyone have a better solution?
Thanks!
The idea of that accepted answer is different: you put A outside notstd namespace and then use qualified notstd::to_string instead of unqualified to_string. That is:
namespace notstd {
// ...
}
class A {
friend std::string to_string(const A&);
};
A a;
std::cout << notstd::to_string(a);
Now your code won't compile if there is no friend function. Moreover, you need only one friend function (taking const A&), because notstd::to_string(T&&) won't be present in the overload set inside adl_helper::as_string(T&&).
Putting A inside notstd screws everything up. You have infinite recursion problem and you need two friends to handle both A and const A cases in the presence of notstd::to_string(T&&) candidate: if only one friend is defined, that candidate is a better match in one of the cases because const qualifier should be added/dropped to invoke the friend function.

Unable to dereference pointer

I am writing an implementation of the Haskell Maybe Monad in C++11.
However I got stuck when I tried to test the code. When I construt a value of the type with the pseudo constructor Just and then try to evaluate it with using the function fromJust (that should just "unpack" the value placed inside the Maybe) the program stops and eventually terminates silently.
So i tried to debug it; here is the output for the code of testMaybe.cpp:
c1
yeih2
value not null: 0xf16e70
I added a couple of print statements to evaluate where the program stops, and it seems to stop at the exact point where I dereference the pointer to return the value. (I have marked it in the code.)
At first I thought that the value in the maybe might have been deconstructed by the time i want to dereference the pointer, which, to my understanding, would result in undefined behaviour or termination. However, I was unable to find the place where that would have happened.
Can you please give me a hint on why this is happening?
testMaybe.cpp:
#include<iostream>
#include "Maybe.hpp"
using namespace std;
using namespace Functional_Maybe;
int main() {
Maybe<string> a{Maybe<string>::Just("hello") };
if(!isNothing(a)) cout << "yeih2 " << fromJust(a) << endl;
return 0;
}
Maybe.hpp
#pragma once
#include<stdexcept>
#include<iostream>
using namespace std;
namespace Functional_Maybe {
template <typename T>
class Maybe {
const T* value;
public:
Maybe(T *v) : value { v } {} //public for return in join
const static Maybe<T> nothing;
static Maybe<T> Just (const T &v) { cout << "c1" << endl; return Maybe<T> { new T(v) }; }
T fromJust() const {
if (isNothing()) throw std::runtime_error("Tried to extract value from Nothing");
cout << "\nvalue not null: " << value << " " << *value << endl;
// ^ stops here
return *value;
}
bool isNothing() const { return value==nullptr; }
~Maybe() { if (value != nullptr) delete value; }
};
template <typename T>
bool isNothing(Maybe<T> val) {
return val.isNothing();
}
template <typename T>
T fromJust(Maybe<T> val) {
return val.fromJust();
}
}
You class template Maybe owns resources (the dynamically allocated T), but does not follow the Rule of Three: the (implicitly defined) copy and move operations do shallow copies only, which leads to use-after-free and double-free problems. You should either implement proper copy and move operations (cosntructors and assignment operators) for your class, or use std::unique_ptr<const T> as the type of value, and remove your manual destructor (thereby following the preferred Rule of Zero).
Side note: have you looked into std::optional (or, in pre-C++17 versions, boost::optional)? They seem to be doing something very similar (or even identical) to your proposed class, and you might want to use them instead (or use them internally in your class if that suits you better). They might even be more efficient, using small object optimisation to avoid dynamic memory allocation in some cases.

Use pack parameter in template vector & Lambda - c++

I am trying variadic template pack inside lambda function with vector.
Learning the c++ programming langage book, it's said "If you need to capture a variadic template argument, use ..." (with a simple example).
I am not able to compile it, my aim is just to be able to print the variadic type inside my lambda:
#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;
template<typename... vectType>
void printAllElements(vector<vectType...>& nbList, ostream& output, int nbToTest){
for_each(begin(nbList),end(nbList),
[&vectType...](vectType... x){
vectType... v;
output << x << endl;
output << typeid(v).name() << endl;
}
);
}
int main() {
vector<int> myVect {10,20,156,236};
printAllElements(myVect,cout, 4);
}
StackTrace:
learnC++/main.cpp:10:7: error: 'vectType' in capture list does not name a variable
[&vectType...](vectType... x){
^
learnC++/main.cpp:11:15: error: only function and template parameters can be parameter packs
vectType... v;
^
learnC++/main.cpp:12:7: error: variable 'output' cannot be implicitly captured in a lambda with no capture-default specified
output << x << endl;
Regards
Update 1:
Ok so I update this question to add the two code of Stroustrup I concataned with his advices.
Lambda & Vector
Void print_modulo(const vector<int>& v, ostream& os, int m){
for_each(begin(b),end(v),
[&os,m](int x){ if (x%m==0 os << w << '\n');}
}
The version using lambda is a clear winner. But if you really want a
name, you can just provide one ..
Capture template
If you need to capture template argument, use ... For example:
template<typename... Var>
void algo(int s, Var... v){
auto helper = [&s,&v...]{return s*(h1(v...)+h2(v...);)};
}
h1 & h2 are simple examples functions
I will try to explain what is wrong in your code, but to be truly understood, you need to read some good books.
std::vector is a homogeneous container, all its elements have only one same type. This vector<vectType...> is pointless. For example, std::vector<int> has all int elements; there is no such a thing std::vector<int, double, short>. Thus if you want to print the typeid for some different types, your approach is wrong from the beginning.
In your own code [&vectType...], those vectType are types, that's also pointless. In your quoted code [&s,&v...], those v are variables, thus correct.
Such vectType... v; declaration is invented by yourself, it's not C++.
The following is a simple example in C++14 if you want to print variables' typeid in a generic way:
#include <iostream>
template<class... Args>
void print_all_typeid(Args const &...args)
{
auto dumb = [] (auto... x) {};
auto printer = [] (auto const &x) {
std::cout << typeid(x).name() << "\n";
return true;
};
dumb(printer(args)...);
}
int main() {
print_all_typeid(std::cin, 12, std::cout, 1L, 2.0, 3.f);
}

c++ - help understanding differences between function and class templates

I'm trying to implement a C++ program that, given a list of elements, prints out unique elements from the list.
I know C a lot better than C++, but I am starting out on C++ practically (coding) only now.
I've only read up on C++ concepts what templates are and I'm comfortable with function templates but I just read up on class templates and I think I'm getting confused as to where to use which one, as applied to the scenario below.
Here's what I've written so far (*note that the function isUnique is supposed to be doing something else but I'm writing just verifiable actions inside it for now):
cppArrays.h
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
template <class T> class cpparray{
private:
int size;
vector<T> elems;
public:
cpparray(int);
~ cpparray();
int isUnique(T arr);
};
template <class T> cpparray<T>::cpparray(int size)
{
vector<T> elems(size);
cout << "Object created with size " << elems.size()<< "\n"<< endl;
}
template <class T> cpparray<T>::~cpparray()
{
cout << "Object del\n" << endl;
}
template <class T> int cpparray<T>::isUnique(T arr)
{
return arr.size();
}
cppArrays.cc
#include "cppArrays.h"
int main()
{
cpparray<int> a(10) ;
//a.push_back(1);
//a.push_back(2);
//cout << a.size() << a.begin() << a.end() << endl;
int b = isUnique(a);
return 0;
}
Details:
[1] I'm trying to use templates, because I want my vector to be able to be instantiated with any data type - char/float/int.
[2] I realize that in using a class template, by calling
cpparray<int>a(10);
I end up declaring an object a of class "cpparray" whose type is Integer. Is this correct?
If yes, then a.push_back(1) wouldn't work because it doesn't refer to the member variable but the object itself and hence it's understandable I'll get a compile time error saying no member named push_back in cpparray.
But this is making it even more difficult for me to
[1] understand when to use class template as opposed to function template,
[2] How do I initialize this vector in the template class and work with it, given my objective?
Your constructor should use initialization list to init members,
like this:
template <class T> array<T>::array(int sz) : size(sz), elems(sz)
{
cout << "Object created with size " << elems.size()<< "\n"<< endl;
}
What you did is to declare the vector as local in the constructor, init it to size, and the local vector was destroyed at the end of block.
Use a class template when you need a generic type that has compile-time variable properties. Template parameters can be types and constants, e.g.
template<typename T, size_t Size>
class MyArray {
T elements_[Size];
public:
MyArray() {}
// ...
};
Use a function template when wish to write a generic function that can be applied to various types/parameters:
#include <cstdio>
#include <iostream>
template<size_t BufSize, typename... Args>
int strprintf(char(&buf)[BufSize], const char* fmt, Args&&... args)
{
static_assert(BufSize > 0, "Buffer too small");
static_assert(BufSize < (1 << 31), "Buffer too large");
return snprintf(buf, BufSize, fmt, std::forward<Args>(args)...);
}
int main() {
char buf[16];
int printed = strprintf(buf, "hello world %s so long", "oversized load");
std::cout << buf << "\n";
}
http://ideone.com/SLUQX3
The above is an example of how you can replace one of those old vsnprintf forwarding printf-type functions; doing all the legwork at compile time makes it drastically more efficient.
BufSize can be deduced by the compiler because the type of buf is char[16]; it can capture the source by reference, the type is char and the array size - the template variable - is 16.
It's also possible to have a templated member function of a templated class:
template<typename T>
class Foo {
T t_;
public:
Foo() : t_() {}
Foo(const T& t) : t_(t) {}
template<typename RhsT>
bool is_same_size(const RhsT& rhs) {
return t_.size() == rhs.size();
}
};
This example will only work for instances when both T and RhsT have a size() member function, leading to the following:
Foo<vector<int>> fvi;
Foo<list<double>> fld;
fvi.is_same_size(fld); // fine
Foo<int> fi;
fvi.is_same_size(fi); // compiler error

When is a reference to function useful?

A reference to array parameter is useful because it prevents decay, allowing you to deduce the size of an array.
template <typename T, std::size_t N>
constexpr std::size_t array_size(T (&) [N]) noexcept
{
return N;
}
But when is this useful?
template<typename T>
void foo(T& t)
{
// we now have a reference to bar
}
void bar() { }
int main()
{
foo(bar);
}
When do we care about preventing function to pointer decay? I'm asking about when this is useful, not why it isn't disallowed.
Like with objects, we us a pointer if “no object” (“no function”) is a sensible value and a reference if we wish to make sure that (unless somebody insists to shoot himself in the foot) there is always a valid object (function) referred to.
Consider the following poor man's function wrapper (wealthy man goes over there).
template<typename>
class Function; // never defined
template<typename ReturnT, typename... ArgT>
class Function<ReturnT(ArgT...)>
{
private:
ReturnT (*func_)(ArgT...);
public:
// Will also accept a 'nullptr'
Function(ReturnT (*func)(ArgT...)) noexcept : func_ {func}
{
}
ReturnT
operator()(ArgT... args)
{
return this->func_(args...);
}
};
Now we can write the following program, which works just fine.
#include <iostream>
int
add(int a, int b)
{
return a + b;
}
int
main()
{
Function<int(int, int)> f {add}; // ok
std::cout << f(5, 7) << std::endl; // ok, prints 12
}
However, we can also write the following program, which doesn't work so nice.
int
main()
{
Function<int(int, int)> f {nullptr}; // compiles fine
std::cout << f(5, 7) << std::endl; // compiles fine, crashes at run-time
}
Instead, if we had replaced the (*func) with (&func) in the template's definition,
// Won't accept a 'nullptr'
Function(ReturnT (&func)(ArgT...)) noexcept : func_ {func}
{
}
the line
Function<int(int, int)> f {nullptr}; // compile-time error
would have triggered a compile-time error.
To me the difference is like between normal variable reference and a pointer. References are nicer to deal with than pointers.
I had a use-case when I to customize a parts of an algorithm. It had some phases and I could switch between several options for each phase. This is of course achievable with pointers, but hey, you can use pointers instead of references pretty much everywhere too.
It looked similar to this:
#include <iostream>
using namespace std;
class Foo
{
public:
Foo(void(&t)()) : barish(t) { };
void fun() {barish();};
private:
void(&barish)();
};
void bar() { cout << "meow\n";}
void bark() { cout << "woof\n";}
int main()
{
Foo foo1(bar);
Foo foo2(bark);
foo1.fun();
foo2.fun();
}
You can customize fun and don't have to deal with dereferencing and asserts that it's not null.