C++ templates: prevent instantiation of base template - c++

I have an interface
std::string
get_string(Source const &s, std::string const &d);
int
get_int(Source const &s, int const &d);
bool
get_bool(Source const &s, bool const &d);
which I'd like to change to
template<class T>
T
get(Source const &s, T const &d);
But there's no sensible base template, so the actual base definition is a legal but useless (return d;). What can I do to force compile-time failure if the base is instantiated? Is there an idiomatic solution for this situation?

Don't define the template, just declare it and define the three specializations.
template <typename T>
T get(Source const &, T const &);
template<>
std::string get(Source const &s, std::string const &d) {
return d + s.stringval(); // or whatever
}
[Edit: removed stuff about overloads - just for once, template function specialization does actually seem to be better. Who woulda thunk?]

just do
string get(source, string);
int get (source, int);
bool get(source, bool);

If you are willing to pay for run-time polymorphism, you can do this...
template <typename T>
class Interface
{
virtual T get(Source const &s, T const &d) = 0;
};
class StringInterface : public Interface<std::string>
{
virtual std::string get(Source const& s, std::string const& d);
};
// etc.
Since your base is an abstract class, you will get a compile-time failure if you try to instantiate it directly.

Declare the baseclass (t) as abstract, that way an instance can never be created of that class.

Related

Overloading operator[] for a container class with template objects

First of all, I have a template class that looks like this:
template <typename T>
class Configurable
{
public:
//protected:
T var_value;
std::string var_name;
std::string var_type;
Configurable()
: var_value(0), var_name("unnamed"), var_type("undefined")
{}
Configurable( T v_value, std::string v_name, std::string v_type )
: var_value(v_value), var_name(v_name), var_type(v_type)
{}
std::string get_name() {return var_name;}
};
I also have a container class named Config which has a couple of different Configurable lists for storage of Configurable ints, bools and floats. I want to overload the [] operator of Config so that it returns a Configurable with the given name (regardless of the type) from one of the lists, but this doesn't seem to work:
template <typename T>
Configurable<T>& operator[] ( const std::string v_name_arg );
The compiler returns an error of 'no match for operator[]'. So my question is - how can I make this work? Is it even possible to do it using templates or should I find a different approach with inheritance?
EDIT: Sorry for all the confusion. Here's the container class I'm talking about:
class Config
{
public:
//private:
std::list < Configurable<int> > list_int;
std::list < Configurable<float> > list_float;
std::list < Configurable<double> > list_double;
std::list < Configurable<bool> > list_bool;
//public:
Config(){}
template <typename T>
Configurable<T>& operator[] ( const std::string &v_name_arg );
};
The problem with declaring the templated operator[] without any argument that depends on the template parameter is that the compiler cannot determine the type T from a call in the form config["name"].
One solution, considering code readability, would be changing the operator[] to a method such as:
template <typename T>
Configurable<T>& get ( const std::string v_name_arg );
Then, the call should be written like:
config.get<int>("name")
Also, consider passing the string by reference (const std::string&) to avoid unnecessary copies of a std::string passed to the method/operator.
As described above, it's a syntax error.
WHen you write:
template <typename T>
Configurable<T>& operator[] ( const std::string v_name_arg );
You try to define a free standing operator[] as if it would be a free standing function.
But according to your explanations, operator[] should be a member of your container Config.
So the its definition should look somewhat like:
template <typename T>
class Config {
//...
public:
Configurable<T>& operator[] (const std::string v_name_arg) { /* return a ref to a Configurable */ };
};
With such a definition, the stuff compiles, and you can use it for example with :
int main()
{
Configurable<int> c;
Config<int> cfg;
auto a = cfg["test"];
}

C++ operator[] overloading with template accessing boost::variant

I've this class with a map attribute which values are boost::variant.
typedef boost::variant<char, int, bool, unsigned short, float, timeval, double > MultiType;
class A {
public:
template<class T>
T& operator[](const std::string& key) {
return boost::get<T>(map_[key]);
}
template<class T>
std::string keyTypeToString(const std::string& key) {
std::stringstream ss;
ss << boost::get<T>(map_[key]);
return ss.str();
}
private:
std::map<std::string, MultiType> map_;
};
From main:
A a;
a["param"];
Compiler report this errors:
../src/main.cpp:8:25: error: no match for ‘operator[]’ in ‘a["param"]’
../src/main.cpp:8:25: note: candidate is:
../src/util/A.h:53:5: note: template T& A::operator[](const string&)
Maybe I'm missing something trivial, but I can't understand where I'm wrong..
Start with this:
template<class T>
T& get(const std::string& key) {
return boost::get<T>(map_[key]);
}
You call this like a.get<int>("hello"), where it will get the element "hello" as an int.
Next, write this:
struct pseudo_ref {
std::string const& str;
A* a;
template<typename T>
operator T&()&&{
return a->get<T>(str);
}
template<typename T>
pseudo_ref operator=( T&& t ) && {
a->get<typename std::decay<T>::type>(str) = std::forward<T>(t);
return {str, a};
}
pseudo_ref(pseudo_ref const&)=delete;
pseudo_ref& operator=(pseudo_ref const&)=delete;
pseudo_ref( std::string const& s, A* a_ ):str(s), a(a_) {}
};
then back in A:
pseudo_ref operator[](std::string const& str) {
return {str, this};
}
and we get [] that magically converts for you, so long as you assign to it/read from it using the exactly correct type.
This is somewhat dangerous, but cool.
If you want a const pseudo_ref, you need another class to represent it (with no = and operator T const& instead of operator T&).
In practice, this kind of malarkey is rarely worth it.
I wrote this in C++11, because writing it in C++03 is slightly more painful (and runs into lifetime issues with pseudo_ref -- they still exist if you have an auto&& x = a["hello"]), and less pain is good.
class A {
public:
class proxy {
friend class A;
private:
MultiType& it;
proxy(MultiType& it): it(it) {}
public:
template<typename T>
operator T&() {
return boost::get<T>(it);
}
};
proxy operator[](const std::string& key) {
return proxy(map_[key]);
}
private:
std::map<std::string, MultiType> map_;
};
EXPLANATION:
I can see that Yakk was trying similar thing. I have encapsulated the MultiType& from map_[key] in the proxy and then left the work on conversion (type-cast) operator. That's all.
Simple a[""] without assignment gets you the proxy.
double d = a["double"] will try to convert the proxy to double and thus call proxy::operator double&() (I had to test it because I was not sure if the type deduction will work as it is or will need some more work - well, it works!)
AFTERNOTE: It was not clear from the question and code provided what operations are allowed. We can modify the proxy to allow other operations or make it more readonly by changing the signature of type-conversion operator to return const T& instead.
Allowing modification leads to question: why not using MultiType& directly? (returning it from A::operator[]) And that leads to question: why class A at all?
AFTERNOTE #2: boost::variant does not have type-conversion operator and there must have been a reason for it. Think about this code:
int i = a["double"]
Runtime exception! I think that best solution would be to sub-class the MultiType and define type-conversion operator there (while checking boost::variant::which()).
ASSIGNING TO ALREADY PRESENT NAMES:
class A { ...
class proxy { ...
template<class T> proxy& operator=(const T& rhs) {
it = rhs; return *this; }
...but the above can only work if we already have some value in the map.
class A { ...
A() { map_["pi"] = 3.14; } ...
a["pi"] = 3.1415;
COMPLETE REDISIGN:
class MultiType: public boost::variant<int, double, ...> {
public:
template<class T> operator T() {
switch(which()) {
case 0: return boost::get<int>(*this);
case 1: return boost::get<double>(*this);
...
Now we can use std::map<std::string, MultiType> directly (without class A or any proxy).
template<class T>
T& operator[](const std::string& key) {
return boost::get<T>(map_[key]);
}
There's no way for the compiler to deduce T from a call like a["param"];. You'd need to specify it explicitly
a.operator[]<int>("param");
which I doubt is what you're after, but what do I know.

How to replace a functor call by a lambda call in C++11?

I have an abstract template class
template <typename T>
class Predicate1
{
public:
Predicate1();
virtual ~Predicate1();
virtual bool operator() (const T item) const = 0;
};
an implementation
class Pred : public Predicate1<string>
{
public:
virtual bool operator() (const string item) const;
};
bool Pred::operator()(const string item) const
{
return item == "";
}
and a template class with a method filter that takes a predicate:
template <typename T>
class TList : public boost::enable_shared_from_this<TList<T> >
{
public:
typedef boost::shared_ptr<const TList<T> > List;
const List filter(const Predicate1<T>& p) const;
...
};
I then use the filter as follows:
int main(int argc, char *argv[])
{
const TList<string>::List l1 = ...;
const TList<string>::List l2 = l1->filter(Pred());
}
which works OK.
However, I do not know how to replace the functor with an anonymous function.
I have tried:
const TList<string>::List l2 =
l1->filter([] (const string item) -> bool { return item == ""; });
The anonymous function has, as far as I understand, the same signature as the
functor's () operator, so it should work. Instead, I get the compiler error:
error: no matching function for call to ‘TList<std::basic_string<char> >::filter(main(int, char**)::<lambda(std::string)>) const’
note: candidate is: const TList<T>::List TList<T>::filter(const Predicate1<T>&) const [with T = std::basic_string<char>, TList<T>::List = boost::shared_ptr<const TList<std::basic_string<char> > >]
So, somehow the types seem to be incompatible, but I do not understand if I have overlooked something or what I am doing wrong. Or in other words, how do I have to declare the filter method so that it can (only) accept an anonymous function with signature
const string -> bool
or, in general, const T -> bool?
Instead of writing your own interface classes, use std::function:
const List filter(const std::function<bool (const T)>& p) const;
std::function has constructors from lambdas and functors, so you can continue to use Pred.

Pointer to member function in template class

I want to eliminate the process consumed by a decision using pointers to member functions. I need to give user options to turn on or off domain checking for a function defined on a limited continues domain.
It's ok to have pointers to member functions when not using templates. But here I have to generalize my implementation.
Specifically I have three member functions in my class:
1.value is a member function returns value calculated by the member that function points to. The function is a function pointer wich points to either checkedValue or uncheckedValue.
2.checkedValue is a member function that calculates and returns the result, if input is in specified range. else throws a std::domain error.
3.uncheckedValue calculates and returns the result, regardless of domain.
template <typename T>
class IO
{
private:
typedef T (IO<T>::*functionPtr)(const std::string& name, const T& input) const;
functionPtr function;
bool domainValidation;
void setDomainValidation(const bool& value);
//...
public:
// ...
T value(const std::string& name, const T& input) const;
T uncheckedValue(const std::string& name, const T& input) const;
T checkedValue(const size_t& index, const T &input) const;
};
// Implementation:
template <typename T>
void IO<T>::setDomainValidation(const bool &value)
{
domainValidation = value;
if(domainValidation)
{
// function points to checkedValue()
function = & IO<T>::checkedValue; // Here I got undefinded reference error
}
else
{
// function points to uncheckedValue()
}
}
template <typename T>
T IO<T>::value(const string &name, const T &input) const
{
return (this->*function)(name,input);
}
template <typename T>
T IO<T>::uncheckedValue(const string &name, const T &input) const
{
// simply calculate and return the result
}
template <typename T>
T IO<T>::checkedValue(const string &name, const T &input) const
{
// if input is in domain, calculate and return the result
// else throw a std::domain error
}
This looks like a typo to me: The function signature of the function pointer is
...(const std::string &, const T &) ...
The signature of checkedValue is
...(const size_t &, const & T) ...
If the signature is changed to match the function pointer, the code sample compiles for me. Concerning the performance: Are you sure (as in have profiled or looked at the assembly) that the simple if-clause inside the value-method is worse than the (possible) indirection through a function pointer? Obviousl, the call to checked or uncheckedValue cannot be inlined.
Your function has signature
T checkedValue(const size_t& index, const T &input) const;
but not
T IO<T>::checkedValue(const string &name, const T &input) const;
Note the difference between the types of the first parameter.

What are C++ non-template members as used in the Barton-Nackman trick?

From wikipedia:
// A class template to express an equality comparison interface.
template<typename T> class equal_comparable
{
friend bool operator==(T const &a, T const &b) { return a.equal_to(b); }
friend bool operator!=(T const &a, T const &b) { return !a.equal_to(b); }
};
class value_type
// Class value_type wants to have == and !=, so it derives from
// equal_comparable with itself as argument (which is the CRTP).
: private equal_comparable<value_type>
{
public:
bool equal_to(value_type const& rhs) const; // to be defined
};
This is supposed to be the Barton-Nackman, that could achieve compile-time dimensional analysis (checking if some operations applied to variables end up in comparable numbers, like speed comparable to space/time but no acceleration).
Could anyone explain me how, or at least explain me what are the NON-TEMPLATE members?
Thanks
The rules of the language have changed since the pattern was invented, although care was taken not to break it. In other words, as far as I can tell, it still works but for different reasons than it originally did. I don't think I would base an attempt at dimensional analysis on this pattern as I think there are better ways of doing that today.
I also think the example is too trivial to be helpful. As already stated the instantiation of equal_comparable<value_type> causes operator== and operator!= for value_type to appear. Since they are non-members it doesn't matter that the inheritance is private, they're still eligable for selection when resolving a call. It's just hard to see the point in this example. Let's say however, that you add a template parameter to equal_comparable and a few other things:
template<typename U, typename V> class equal_comparable
{
friend bool operator==(U const &a, V const &b) { return a.equal_to(b); }
friend bool operator!=(U const &a, V const &b) { return !a.equal_to(b); }
};
class some_other_type
{
bool equal_to(value_type const& rhs) const;
};
class value_type
: private equal_comparable<value_type>, // value_type comparable to itself
private equal_comparable<some_other_type> // value_type comparable to some_other_type
{
public:
bool equal_to(value_type const& rhs) const;
bool equal_to(some_other_type const& rhs) const;
};
Disclaimer: I have no idea if this is the way it's supposed to be used but I'm reasonably sure that it would work as described.
These are actually nontemplate nonmembers - the comparison operators in the base template - they get used by the ADL for the derived class. A template member would be something like:
class C
{
...
template < typename T > void DoGreatStuff( T t ) { ... }
...
};
The instantiation of equal_comparable<value_type> in value_type class causes the compiler to generate two comparison functions:
friend bool operator==(value_type const &a, value_type const &b) { return a.equal_to(b); }
friend bool operator!=(value_type const &a, value_type const &b) { return !a.equal_to(b); }
These functions are nontemplate since they do not depend on any template parameter, but they are also nonmembers since they are declared as friend.