C++ - Why type casting in Duck Typing - duck-typing

I tried to understand duck typing which is achieved by using templates and SFINAE.
For clear detail please see http://p-nand-q.com/programming/cplusplus/duck_typing_and_templates.html
template <typename T> class repr_type
{
public:
repr_type(const T& o)
:
m_o(o)
{
}
std::string as_string() const
{
return call_as_string<T>(nullptr);
}
private:
template <class C> std::string call_as_string(decltype(&C::as_string)) const
{
return m_o.as_string();
}
template <class C> std::string call_as_string(...) const
{
return string::format("%p", &m_o);
}
const T& m_o;
};
template <typename T> std::string as_string(const T& o)
{
return repr_type<T>(o).as_string();
}
My Question is why it is typecasted to repr_type, in that global templated function as_string(). In the article itself, they mentioned reason as " I cannot also pass in the object instance: so I need a proxy object ". But I couldn't understand. Please explain with other simple example.

Related

QT static library how to export template

I have a helper class that needs a template definition and works fine locally.
Now I try to put it in a static libary and use it in another program.
But when I use it, the linker reports a non-existent symbol. Is it possible to export the template definition somehow?
core_global.h
#pragma once
#include <QtCore/qglobal.h>
#define CORE_EXPORT Q_DECL_EXPORT
helper.h
#pragma once
#include "core_global.h"
template <typename E>
class CORE_EXPORT Helper
{
public:
Helper();
E fromString(const QString& text);
QString toString(E value);
};
helper.cpp
#include "Helper.h"
template<typename E>
Helper<E>::Helper()
{
}
template<typename E>
E Helper<E>::fromString(const QString& text)
{
// do something
return {};
}
template<typename E>
QString Helper<E>::toString(E value)
{
//do something
return {};
}

Boost.Container flat_map and std::string_view

For some time I’ve used Boost’s flat_map as my go-to associative collection, for the reasons explained cited on their documentation intro, and (originally) the fact that it gave newer features before the compiler’s std implementation, and it was the same across platforms.
Now, I wanted to start using string_view to prevent copying strings, when these are taken from substrings of a larger input. string_view points to a range of characters within the larger string, without having to copy them into a new std::string instance.
In reaching for a map to use, I recalled that another progressive feature of Boost.Container that I’ve enjoyed in the past is conformal keys, where you could use anything that compared correctly against the stored key, rather than converting to the actual type of key.
But now I can’t find any mention of that in the documentation. I know the std::map can do that now (since C++14) but I’d rather use the flat_map for tiny collections.
What could I have seen that allowed this flexibility, years ago, if it’s not apparent in boost::flat_map::insert etc.? What are good flat collections to use now with up-to-date compilers?
Support for polymorphic lookup functions has been added only recently to Boost.Container. If everything is good, it should be released with Boost 1.68.
In the meantime you can emulate flat associative containers with an ordered std::vector and std::lower_bound.
typedef std::pair< std::string, int > element_type;
std::vector< element_type > map;
struct element_order
{
bool operator()(element_type const& left, element_type const& right) const
{
return left.first < right.first;
}
bool operator()(std::string_view const& left, element_type const& right) const
{
return left < right.first;
}
bool operator()(element_type const& left, std::string_view const& right) const
{
return left.first < right;
}
};
auto find_element(std::string_view const& key)
{
auto it = std::lower_bound(map.begin(), map.end(), key, element_order());
if (it != map.end() && it->first == key)
return it;
return map.end();
}
Perhaps this is not what you are referring to, but if you use std::string_view as the key type, all operations already work via the implicit conversion to std::string_view:
Live On Coliru
#include <boost/container/flat_map.hpp>
#include <string_view>
int main() {
boost::container::flat_map<std::string_view, int> m {
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "four", 4 },
};
std::string key = "one";
auto one = m.at(key);
auto range = m.equal_range(key);
auto it = m.find(key);
m[key] = 1;
}
The Inverse
Here you'd actually need to use a container that supports compatible-key lookup indeed. It doesn't need to be overly complicated to roll one:
Here's one:
Live On Coliru
#include <initializer_list>
#include <algorithm>
#include <utility>
#include <stdexcept>
#include <boost/container/small_vector.hpp>
template <typename K, typename V, typename Cmp = std::less<K>, typename Storage = boost::container::small_vector<std::pair<K, V>, 10> >
struct flat_map {
using key_type = K;
using mapped_type = V;
using key_compare = Cmp;
using storage = Storage;
using value_type = typename storage::value_type;
using iterator = typename Storage::iterator;
using const_iterator = typename Storage::const_iterator;
struct value_compare {
key_compare _cmp;
template <typename A, typename B>
bool operator()(A const& a, B const& b) const { return _cmp(access(a), access(b)); }
private:
static auto& access(value_type const& v) { return v.first; }
template <typename Other>
static auto& access(Other const& v) { return v; }
} _cmp;
storage _data;
flat_map(std::initializer_list<value_type> i) : _data(i) {}
iterator begin() { return _data.begin(); }
iterator end() { return _data.end(); }
const_iterator begin() const { return _data.begin(); }
const_iterator end() const { return _data.end(); }
template <typename Key>
mapped_type& operator[](Key&& key) { return find(std::forward<Key>(key))->second; }
template <typename Key>
mapped_type const& operator[](Key&& key) const { return find(std::forward<Key>(key))->second; }
template <typename Key>
iterator find(Key&& key) {
auto r = equal_range(std::forward<Key>(key));
return (r.first == r.second)? end() : r.first;
}
template <typename Key>
const_iterator find(Key&& key) const {
auto r = equal_range(std::forward<Key>(key));
return (r.first == r.second)? end() : r.first;
}
template <typename Key>
mapped_type& at(Key&& key) {
auto r = equal_range(std::forward<Key>(key));
if (r.first == r.second) throw std::out_of_range("key");
return r.first->second;
}
template <typename Key>
mapped_type const& at(Key&& key) const {
auto r = equal_range(std::forward<Key>(key));
if (r.first == r.second) throw std::out_of_range("key");
return r.first->second;
}
template <typename Key>
auto equal_range(Key&& key) { return std::equal_range(begin(), end(), std::forward<Key>(key), _cmp); }
template <typename Key>
auto equal_range(Key&& key) const { return std::equal_range(begin(), end(), std::forward<Key>(key), _cmp); }
};
It supports precisely the inverse of the first scenario (given the comparator of std::less<>):
#include <string_view>
#include <string>
int main() {
flat_map<std::string, int, std::less<> > m {
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "four", 4 },
};
std::string_view key = "one";
auto one = m.at(key);
auto range = m.equal_range(key);
auto it = m.find(key);
m[key] = 1;
}

create & post the customized Qevent

I have to create an 2 custom events.
I followed this link & made my code :--
Is there a cleaner way to register Qt custom events?
Is it the right way to create & post & pass some data(Qstring) to the customized event ?
===========================================================
Edit code as per Kuba Ober sugession :---
Mainwindow.h :--
UpdateEvent *myUpdateEvent ;
ClearEvent *myClearEvent ;
Mainwindow.c :---
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
i =0;
myUpdateEvent = new UpdateEvent("hello");
myClearEvent = new ClearEvent("bye");
QCoreApplication::postEvent(this, myUpdateEvent);
QCoreApplication::postEvent(this, myClearEvent);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
qDebug() << "oo";
if (UpdateEvent::is(event)) {
UpdateEvent *tempUpdateEvent = static_cast<UpdateEvent *>(event);
qDebug() << tempUpdateEvent->value();
}
else if (ClearEvent::is(event)) {
ClearEvent *tempClearEvent = static_cast<ClearEvent *>(event);
qDebug() << tempClearEvent->value();
}
return true;
}
event.h file :--
template <typename T> class StringEvent : public QEvent
{
QString m_str;
public:
explicit StringEvent(const QString val) : QEvent(staticType()), m_str(val)
{
}
QString setvalue(QString val)
{
m_str = val;
}
QString value() const
{
return m_str;
}
static QEvent::Type staticType()
{
static int type = QEvent::registerEventType();
return static_cast<QEvent::Type>(type);
}
static bool is(const QEvent * ev)
{
return ev->type() == staticType();
}
};
class UpdateEvent : public StringEvent<UpdateEvent>
{
public:
explicit UpdateEvent(QString val): StringEvent(val)
{
}
};
class ClearEvent : public StringEvent<ClearEvent>
{
public:
explicit ClearEvent(QString val): StringEvent(val)
{
}
};
why eventFilter is not triggered ? And i am not able to see debug message on postevent ?
I can only comment on the code smell of your event implementation, since it is at this point unclear why would you need to send events to an edit control within a window. The latter is bad design.
Your Event class is needlessly complicated. You should be setting up all values inside the event during construction, and they should be accessible through read-only accessors afterwards. The extra event type seems to be an unnecessary gimmick as well.
Here's how I'd do it, using a metafactory pattern (a name I just coined, perhaps there's a better/existing name for it?). This solves the need for explicit derived class constructor injection that'd be otherwise needed.
I've split up the metafactory into small constituent classes for ease of understanding.
// A type-identifier-generating wrapper for events
template <typename D> class EventWrapper : public QEvent {
public:
EventWrapper() : QEvent(staticType()) {}
static QEvent::Type staticType() {
static QEvent::Type type = static_cast<QEvent::Type>(registerEventType());
return type;
}
static bool is(const QEvent * ev) { return ev->type() == staticType(); }
static D* cast(QEvent * ev) { return is(ev) ? static_cast<D*>(ev) : 0; }
};
// The generic event metafactory for C++98 (doesn't need C++11)
template <typename D, template <typename> class Carrier> class EventMF {
class EventFwd;
class Final;
class FinalWrapper : public EventWrapper<EventFwd>, public virtual Final {};
public:
// EventFwd is a class derived from Event. The EventWrapper's cast()
// will cast to a covariant return type - the derived class. That's OK.
typedef Carrier<FinalWrapper> Event;
private:
class EventFwd : public Event {};
class Final {
friend class FinalWrapper;
friend class Carrier<FinalWrapper>;
private:
Final() {}
Final(const Final &) {}
};
};
// A string carrier
template <typename B> class StringData : public B {
QString m_str;
public:
explicit StringData(const QString & str) : m_str(str) {}
QString value() const { return m_str; }
};
// A string event metafactory
template <typename D> class StringEventMF : public EventMF<D, StringData> {};
class Update : public EventMF<Update, StringData> {}; // using generic metafactory
class Clear : public StringEventMF<Clear> {}; // using specific metafactory
#if 0
// This should fail at compile time as such derivation would produce classes with
// duplicate event types. That's what the Final class was for in the matafactory.
class Error : public Update::Event { Error() : Update::Event("") {} };
#endif
int main(int, char**)
{
// Test that it works as expected.
Update::Event update("update");
Clear::Event clear("clear");
Q_ASSERT(Update::Event::staticType() != Clear::Event::staticType());
Q_ASSERT(Update::Event::staticType() == Update::Event::cast(&update)->staticType());
qDebug() << Update::Event::cast(&update)->value();
Q_ASSERT(Update::Event::cast(&clear) == 0);
qDebug() << Clear::Event::cast(&clear)->value();
Q_ASSERT(Clear::Event::cast(&update) == 0);
}
The Metafactory::Event classes are the custom event classes that derive from QEvent.
The class hierarchy for Update::Event looks as follows (going from the least derived to most derived class):
EventWrapper<EventMF<...>::EventFwd>, EventMF<...>::Final (multiple inheritance)
EventMF<Update, StringData<Update>>::FinalWrapper
StringData<Update> = EventMF<Update, StringData<Update>>::Event
with EventMF<...> being shorthand for EventMF<Update, StringData<Update>.
The line Update::Event update("update") constructs a custom string-carrying event instance, with the constructors called from the last to the first in the list above.
Since EventMF<...> is a metafactory that works at time of compilation only, there is absolutely no need for its instance to exist at runtime. Thus EventMF<...>::EventMF constructor is never called. You can enforce this invariant by deleting the constructor (declaring it private for C++98).
The use within an event handler would look like:
void MainWindow::customEvent(QEvent *event)
{
...
if (Update::Event::is(event)) {
qDebug() << Update::Event::cast(event)->value();
...
}
else if (Clear::Event::is(event)) {
...
}
...
}

How can i store Custom types in QSettings?

From API docs:
Custom types registered using qRegisterMetaType() and
qRegisterMetaTypeStreamOperators() can be stored using QSettings.
How can I do that? I get the error:
too few template-parameter-lists at qRegisterMetaTypeStreamOperators
My code:
class LineUser {
public:
int uId;
QString passwd;
qint8 statusType;
};
Q_DECLARE_METATYPE(LineUser)
QDataStream &operator<<(QDataStream &out, const LineUser &myObj) {
out<<myObj.uId<<myObj.passwd<<myObj.statusType;
return out;
}
QDataStream &operator>>(QDataStream &in, LineUser &myObj) {
in>>myObj.uId>>myObj.passwd>>myObj.statusType;
return in;
}
qRegisterMetaTypeStreamOperators<LineUser>("LineUser");
qRegisterMetaTypeStreamOperators is a function, not a macro.
You need to call it from a .cpp file, e.g. in your main() method

recursive variadic template to print out the contents of a parameter pack

How is it possible to create a recursive variadic template to print out the contents of a paramater pack?
I am trying with this, but it fails to compile:
template <typename First, typename ...Args>
std::string type_name () {
return std::string(typeid(First).name()) + " " + type_name<Args...>();
}
std::string type_name () {
return "";
}
How shall I end the recursion?
There's actually a very elegant way to end the recursion:
template <typename Last>
std::string type_name () {
return std::string(typeid(Last).name());
}
template <typename First, typename Second, typename ...Rest>
std::string type_name () {
return std::string(typeid(First).name()) + " " + type_name<Second, Rest...>();
}
I initially tried template <typename Last> and template <typename First, typename ...Rest> but that was considered ambiguous (Rest can be zero elements). This question then showed me the definitive solution: Compilation Error on Recursive Variadic Template Function
Note, to avoid a bit of code duplication, you could also do:
template <typename Last>
std::string type_name () {
return std::string(typeid(Last).name());
}
template <typename First, typename Second, typename ...Rest>
std::string type_name () {
return type_name<First>() + " " + type_name<Second, Rest...>();
}
You need to use partial specialisation to end the recursion, but since you can't partially specialise free functions in C++, you need to create an implementation class with a static member function.
template <typename... Args>
struct Impl;
template <typename First, typename... Args>
struct Impl<First, Args...>
{
static std::string name()
{
return std::string(typeid(First).name()) + " " + Impl<Args...>::name();
}
};
template <>
struct Impl<>
{
static std::string name()
{
return "";
}
};
template <typename... Args>
std::string type_name()
{
return Impl<Args...>::name();
}
int main()
{
std::cout << type_name<int, bool, char, double>() << std::endl; // "i b c d"
return 0;
}
That first declaration of Impl is just a workaround for a shortcoming in g++ 4.6 (and below). It won't be necessary once it implements variadic templates correctly.
Check it out in action at ideone.com
C++17's if constexpr allows you to do this in one template declaration which is, unlike a lot of the older solutions, pretty easy to understand:
template <typename T, typename ...Args>
std::string type_name() {
if constexpr (!sizeof...(Args)) {
return std::string(typeid(T).name());
} else {
return std::string(typeid(T).name()) + " " + type_name<Args...>();
}
}
As an alternative to non-existing partial specialization for functions, you can use overloading on a typifier class:
#include <string>
#include <iostream>
#include <typeinfo>
template <unsigned int N> struct NumberToType { };
template <typename T>
std::string my_type_name(NumberToType<0> = NumberToType<0>())
{
return std::string(typeid(T).name());
}
template <typename T, typename ...Args>
std::string my_type_name(NumberToType<sizeof...(Args)> = NumberToType<sizeof...(Args)>())
{
return std::string(typeid(T).name()) + " " + my_type_name<Args...>(NumberToType<sizeof...(Args)-1>());
}
int main()
{
std::cout << my_type_name<int, double, char>() << std::endl;
}
As an alternative, you can unpack the parameter pack in-place as in the following example:
#include<string>
#include<iostream>
#include<typeinfo>
template <typename T, typename ...Args>
std::string type_name () {
std::string str = typeid(T).name();
int arr[] = { 0, (str += std::string{" "} + typeid(Args).name(), 0)... };
(void)arr;
return str;
}
int main() {
auto str = type_name<int, double, char>();
std::cout << str << std::endl;
}
Recursion is not required actually to do that.
Use C++17 fold expression:
template <typename ...Args>
std::string type_name () {
return (std::string(typeid(Args).name()) + " " + ...);
}

Resources