"templating" a namespace - c++

I'd like to build something like this:
File 1:
template<typename Vector>
namespace myNamespace {
class myClass1{ myClass1(Vector v) {...} }
}
File 2:
template<typename Vector>
namespace myNamespace {
class myClass2{ myClass2(Vector v) {...} }
}
Of course this is not possible because you cannot template namespaces.
Instead I could use a struct instead of a namespace, but then I cannot spread the namespace functions over several files.
Is there any solution for such a problem?
PS: I know I could template the classes, but then I'd have to specify which vector type I want to use anytime I create a new class.

Following up on your comment:
Instead of writing
using namespace myNamespace<int>;
Just use templated classes and write this instead (or whatever variation):
typedef myNamespace::myClass1<int> myClass1Int;
typedef myNamespace::myClass2<int> myClass2Int;
I tend to think it's better to be explicit about what types are being used rather than trying to do something like import a particular instantiation of a namespace.
Can you more fully describe the problem that makes you think templated namespaces would be useful?
And remember you can always write a make_myClass1 free function to deduce the template type for you.

You cannot do that, but you can provide different namespaces and typedefs (not that I endorse it).
namespace template_impl {
template <typename V>
class myClass1_tmpl {...};
template <typename V>
class myClass2_tmpl {...};
}
namespace myns_Vector1 {
typedef ::template_impl::myClass1_tmpl<Vector1> myClass1;
typedef ::template_impl::myClass2_tmpl<Vector1> myClass2;
}
void foo() {
using namespace myns_Vector1;
myClass1 mc1;
}

Anyway, my classes have several template parameters.
Now I created this approach:
#include <string>
#include <iostream>
namespace myNamespace {
template<typename _integer, typename _string>
struct s {
typedef _integer integer;
typedef _string string;
};
template<class T>
class classA {
public:
static typename T::integer intFunc() { return 1; }
static typename T::string stringFunc() { return "hallo"; }
};
}
int main() {
using namespace myNamespace;
typedef s<int, std::string> types1;
typedef s<unsigned int, char*> types2;
std::cout << classA<types1>::intFunc() << std::endl;
std::cout << classA<types1>::stringFunc() << std::endl;
std::cout << classA<types2>::intFunc() << std::endl;
std::cout << classA<types2>::stringFunc() << std::endl;
}
and I think I'll combine it with Mark B's approach!
Cheers, guys!

Related

How is nested template specialization done C++

I have a templated function defined as:
template<typename TObject> TObject Deserialize(long version, const Value &value)
what I need to do, is to write a specialization which would take vector defined as:
template<typename TNum, int cnt> class Vec
and still has access to cnt and TNum.
I have unsuccesfully tried
template<typename TNum, int cnt> Vec<TNum, cnt> Deserialize<Vec<TNum, cnt>>(long version, Value &value)
resulting in error: illegal use of explicit template arguments
What is the correct way to do it?
Usually, the correct answer to dealing with function templates and needing to partially specialize them, is to simply overload them instead. In this case this trick doesn't work directly because there are no arguments that depend on the template parameter, i.e. the template parameter is explicitly specified and not deduced. However, you can forward along to implementation functions, and make overloading work by using a simple tag struct.
#include <functional>
#include <iostream>
#include <type_traits>
#include <vector>
#include <array>
template <class T>
struct tag{};
template<typename TObject>
TObject Deserialize_impl(long version, tag<TObject>) {
std::cerr << "generic\n";
return {};
}
template<typename T, std::size_t N>
std::array<T,N> Deserialize_impl(long version, tag<std::array<T,N>>) {
std::cerr << "special\n";
return {};
}
template<typename TObject>
TObject Deserialize(long version) {
return Deserialize_impl(version, tag<TObject>{});
}
int main() {
Deserialize<int>(0);
Deserialize<std::array<int,3>>(0);
return 0;
}
Live example: http://coliru.stacked-crooked.com/a/9c4fa84d2686997a
I generally find these approaches strongly preferable to partial specialization of a struct with a static method (the other major approach here) as there are many things you can take advantage with functions, and it behaves more intuitively compared to specialization. YMMV.
While the functional tag-dispatch is a nice approach, here's a class specialization version for comparison. Both have their use, and I don't think either is an inherently regrettable decision but maybe one matches your personal style more.
For any class you write that needs a custom deserialize handler, just write a specialization of the Deserializer class:
#include <iostream>
#include <string>
using namespace std;
using Value = std::string;
// default deserialize function
template <typename TObject>
struct Deserializer {
static TObject deserialize(long version, const Value &value) {
std::cout << "default impl\n";
return TObject();
}
};
// free standing function (if you want it) to forward into the classes
template <typename TObject>
TObject deserialize(long version, const Value &value) {
return Deserializer<TObject>::deserialize(version, value);
}
// Stub example for your Vec class
template<typename TNum, int cnt> class Vec { };
// Stub example for your Vec deserializer specialization
template <typename TNum, int cnt> struct Deserializer<Vec<TNum, cnt>> {
static auto deserialize(long version, const Value &value) {
std::cout << "specialization impl: cnt=" << cnt << "\n";
return Vec<TNum, cnt>();
}
};
int main() {
Value value{"abcdefg"};
long version = 1;
deserialize<int>(version, value);
deserialize<Vec<int, 10>>(version, value);
}
Ideally in this situation, Vec should reflect its own template parameters as members Vec::value_type and Vec::size() which should be constexpr.
If the class fails to provide its own properties in its own interface, the next best thing is to define your own extension interface. In this situation, you can have separate metafunctions (like accessor functions), or a traits class (like a helper view class). I'd prefer the latter:
template< typename >
struct vector_traits;
template< typename TNum, int cnt >
struct vector_traits< Vec< TNum, cnt > > {
typedef TNum value_type;
constexpr static int size = cnt;
};
template<typename TVec> TVec Deserialize(long version, Value &value) {
typedef vector_traits< TVec > traits;
typedef typename traits::value_type TNum;
constexpr static int cnt = traits::size;
…
}
This solution fits into any existing function, and even makes the signatures cleaner. Also, the function is more flexible because you can adapt it by adding traits specializations instead of entire new overloads.

why is it necessary to include template parameter before every function even if we are not using diduced type in the function?

Why are we suppose to use template parameters at front of every function even if we are not using deduced template parameters in the function. As we can see that i am not using template parameter _T in printP() function (around 30) then why it is required to include template syntax at front of this function.
NOTE: This is very simplified version of my big class, and it might look silly because it is very small but, consider a situation where you are using template for only few [2-3] function of your class but you are bound to type (even copy past) this lengthy template syntax at front of every function but i am asking why??.
Is there any way to get of this
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
template<typename _T>
class Thing{
int _p;
_T _k;
public:
Thing(int p, _T k):_p(p),_k(k){}
void printK();
void printP();
};
template<typename _T>
void Thing<_T>::printK(){
cout << _k << endl;
}
template<typename _T>
void Thing<_T>::printP(){
cout << _p << endl; // as we can see that i am not using template paramerter "_T"
} // here in this function then why it is required to include template syntax
int main()
{
Thing<int> a(1,2);
a.printK();
a.printP();
}
Because the function PrintK is member of template class Thing<_T>. For a member function definition outside the class, the function name also includes class name(to which it belongs, here it belongs to Thing), since Thing is template, so function name requires you to provide template parameter (T here).
e.g.
Function definition outside class requires the following syntax
**
return type class name:: function name (argument list)
*
Here class (Thing) is template class, so its name will also require type (like Thing<_T>).
I hope you got my point.
Its usually a good idea to restrict the members and functions of a template class to those that are dependent on the template parameters. Non-dependent members and functions can be put in a separate non=template class (is there a better name?). For example:
#include <iostream>
using namespace std;
class ThingBase
{
public:
ThingBase(int p)
: _p(p)
{
}
void printP();
protected:
int _p;
};
void ThingBase::printP(){
cout << _p << endl;
}
template<typename _T>
class Thing : public ThingBase {
_T _k;
public:
Thing(int p, _T k)
: ThingBase(p),
_k(k){}
void printK();
};
template<typename _T>
void Thing<_T>::printK(){
cout << _k << endl;
}
int main()
{
Thing<int> a(1,2);
a.printK();
a.printP();
}

Dynamic namespace usage based on a template parameter

I don't know if that is feasable at all, but this is what I'd like to achieve : in a templated class I would like to be using the namespace of the template parameter.
eg.
template<class P>
class Foo
{
public:
Foo();
virtual ~Foo();
void doSomething(P&);
void doSomethingElse();
protected:
// There I'm hardcoding "namespace1" but that's what I'd like to
// be possibly dynamic
// (I'm assuming template parameter P = namespace1::Type)
void method1(namespace1::Type1&);
...
void methodN(namespace1::TypeN&);
}
// Again, supposing P == namespace1::Type then I want to be using namespace1
// everywhere in the implementation...
using namespace namespace1;
template<class P>
void Foo<P>::doSomething(P& parameter)
{
...
Type1 type1 = P.getType1(); // There namespace1::Type1 is returned !!
method1(type1);
...
}
template<class P>
void Foo<P>::doSomethingElse()
{
...
TypeN typen; // There I want to instanciate a namespace1::TypeN !!
...
}
...
Of course I don't want to specialize the template and provide a dedicated implementation for every possible P value as well as I'd like to avoid passing all the types like Type1 and TypeN as template parameters since I potentially have lots of them.
Is that possible ?
The project is C++3 based, any boost solution is welcome.
Update
Being the template parameter P itself exactly like any TypeN parameter, this could be the right approach :
template<typename NAMESPACE>
class Foo
{
typedef typename NAMESPACE::Parameter MyParameter;
typedef typename NAMESPACE::Type1 MyType1;
typedef typename NAMESPACE::Type1 MyTypeN;
...
}
Yes and No.
Yes it is possible to deduce secondary types from a primary one, generally using a trait system:
template <typename T> struct Trait { typedef typename T::Secondary Secondary; };
template <typename X>
struct Foo {
typedef typename Trait<X>::Secondary Secondary;
void foo(Secondary const& s);
};
No, you cannot deduce a namespace, and thus cannot use it; but note how by using a local alias (typedef ...) within the class there is no need to.
If you are willing to add a structure inside each namespace that lists all the types you need, you can then rely on ADL to get this structure depending on a template parameter:
#include <iostream>
#include <utility>
namespace Foo_ns
{
struct T1
{
static void m() { std::cout << "Foo_ns::T1" << '\n'; }
};
struct Foo {};
// List of all the types you need from this namespace
struct Types
{
typedef Foo_ns::T1 T1;
};
// dummy function needed for ADL
Types types(...);
}
namespace Bar_ns
{
struct T1
{
static void m() { std::cout << "Bar_ns::T1" << '\n'; }
};
struct Bar {};
struct Types
{
typedef Bar_ns::T1 T1;
};
Types types(...);
}
template <typename T>
void callMOnT1(const T &arg)
{
typedef decltype(types(std::declval<T>())) Types; // ADL kicks in
//typedef typename std::result_of<types(T)>::type Types;
Types::T1::m();
}
int main()
{
callMOnT1((Bar_ns::Bar())); // Bar_ns::T1
callMOnT1((Foo_ns::Foo())); // Foo_ns::T1
}
Unfortunately, this solution using some C++11 features (namely decltype and declval). For some reason, I didn't manage to get the code working with result_of, which is present in Boost (perhaps someone could explain why the code with result_of does not compile?).
No that is impossible,but you can use ugly macro file.
//my_class.inl
template<TYPE_ARG>
struct my_class_t<TYPE_ARG>
{
foo()
{
NS_ARG::bar();
}
}
//my.h
template<class T>
struct my_class_t{};
#define TYPE_ARG T1
#define NS_ARG std
#include "my_class.inl"
#define TYPE_ARG T2
#define NS_ARG boost
#include "my_class.inl"
#undef TYPE_ARG
#undef NS_ARG
That way you will automate class specialisation for different namespaces. Do you really need this? :o)

Reflection in known set of classes

I am trying to iterate through an object hierarchy and the object hierarchy is composed of a known set of classes combined using composition. I would like to build an object model to show the hierarchy / composition graphically. The composition is done based on few rules but it is fluid and flexible.
Quite a few classes (25+) are available and the number of building blocks is increasing. If I search each type in every other type then we have a significantly large number of combinations possible.
I could build a large table where I search for each of the other objects for a given type and recursively build the object model but may be there is a better way and so here I am asking the experts.
Is it possible to know if a function / member variable is present on a particular type at runtime.
My sample code is shown below :
#include <iostream>
struct Generic {};
struct SimpleType {int toString(){return 0;}};
enum ETypeVal{eVal1 = 0, eVal2 = 1, eVal3 = 2};
template <typename ETypeVal val>
struct Hello
{
int toString(){return 0;}
};
template <> struct Hello<eVal2>
{
int toString(){return 1;}
};
template <> struct Hello<eVal3>
{
};
template <class Type>
class TypeHasToString
{
public:
typedef bool Yes;
typedef short No;
static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
private:
template <typename T, T> struct TypeCheck;
template <typename T> struct ToString
{
typedef int (T::*fptr)();
};
template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::toString >*);
template <typename T> static No HasToString(...);
};
int main(int argc, char *argv[])
{
// all this works fine
std::cout << TypeHasToString<Generic>::value << std::endl;
std::cout << TypeHasToString<SimpleType>::value << std::endl;
std::cout << TypeHasToString<Hello<eVal1>>::value << std::endl;
std::cout << TypeHasToString<Hello<eVal2>>::value << std::endl;
std::cout << TypeHasToString<Hello<eVal3>>::value << std::endl;
// Unable to deduce for type that are not known at compile time
// Is it possible to remove this limitation ?
for(int val = eVal1; val <= eVal3; val++)
{
std::cout << TypeHasToString< Hello< (ETypeVal)val > >::value << std::endl;
}
return 0;
}
I've used boost::mpl to do the iteration and printing of the values. Most of this should be possible without any of those, but I heavily recommend using it. Also I've fixed some things in your code. You might also want to use BOOST_HAS_XXX instead of your homebrew solution (your SFINAE style is rather awkward).
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/for_each.hpp>
struct Generic {};
struct SimpleType {int toString(){return 0;}};
enum ETypeVal{ eVal1 = 0, eVal2 = 1, eVal3 = 2};
template <ETypeVal val>
struct Hello
{
int toString(){return 0;}
};
template <> struct Hello<eVal2>
{
int toString(){return 1;}
};
template <> struct Hello<eVal3>
{
};
template <class Type>
class TypeHasToString
{
public:
typedef bool Yes;
typedef short No;
private:
template <typename T, T> struct TypeCheck;
template <typename T> struct ToString
{
typedef int (T::*fptr)();
};
template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::toString >*);
template <typename T> static No HasToString(...);
public:
static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
};
template<typename val>
struct make_hello { typedef Hello< ETypeVal(val::value)> type; };
struct print_seq {
template<typename T>
void operator()(const T&) const {
std::cout << T::value << std::endl;
}
};
int main()
{
using namespace boost::mpl;
// maybe have a last enum here
typedef range_c<int, eVal1, eVal3 + 1>::type range;
// range has no clear so we need the inserter
typedef transform<range, make_hello<_1>, back_inserter< vector0<> > >::type hellos;
typedef transform< hellos, TypeHasToString<_1> >::type booleans;
// namespace for clarity
boost::mpl::for_each<booleans>( print_seq() );
return 0;
}
We don't have run-time reflection in C++. But we have different stuff, that most C++ programmers like better than reflection ;-).
If I understand your question, you need to build some sort of object browser, and you know all your object types. By "you know all your object types" I mean "you won't get an object as something at the other end of a pointer from a dll you didn't code".
So, maybe you can use boost::fusion? Said library is designed for iterating through aggregates, and while doing so, retrieving both data and type of each aggregate member. It is like iterators over struct members, if I have to put it in a flowery way. You can of course use it with your custom types.
You can't know at runtime if you don't at compile time. You have the code to know at compile time for one function. You could just make a macro out of it to have it for any function you want to check. (Macro disclaimer: in this case macros are good, that's how BOOST_MPL_HAS_XXX_TEMPLATE_DEF works).
Alternatively, there's boost::fusion as mentionned by dsign. But I prefer another one: boost::reflect (not actully in boost more info here). The macro syntax is easier (you don't need to mention the type in the macro) and the code is very lightweight. Then there's the feature complete boost::mirror (download here), not yet in boost, that is much more complete and even has a code generator to create the macro calls for you and a java style runtime reflection.
I think that you need the runtime polymorphism in this case. Use interfaces instead of templates for such kind of problems. Interfaces will give the knowledge about of the methods in your object but will say nothing about the member variables. So there is no reflection available in standard c++ (the only thing that c++ provides is the type_info operator which might help you in some cases), you could try to find some extensions for your compiler which will give you the reflection opportunities.

Boost mpl for each and free functions

Why this code does not compile:
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
#include <iostream>
using namespace std;
using namespace boost;
template <class T> // specific visitor for type printing
static void print_type(T t)
{
std::cout << typeid(T).name() << std::endl;
}
typedef mpl::vector<int, long, char*> s;
int main ()
{
mpl::for_each<s>(print_type());
}
I wonder - how to make boost mpl for_each work with free functions from same class?
As stated you need a functor.
The code below includes an additional wrap template that allows the print functor to cope with references.
#include <iostream>
#include <typeinfo>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/placeholders.hpp>
using namespace std;
using namespace boost;
template <typename T>
struct wrap {};
struct print_type
{
template< typename T>
void operator()( wrap<T> ) const
{
cout << typeid(T).name() << "\n";
}
};
typedef mpl::vector<int, long&, char*> s;
int main ()
{
mpl::for_each<s, wrap<mpl::placeholders::_1> >(print_type());
return 0;
}
Note: this code based on examples in the book 'C++ Template Metaprogramming by David Abrahams and Aleksy Gurtovoy
mpl::for_each<s>(print_type());
This is wrong in a couple of ways.
First, print_type is a function that returns void. print_type() is an attempt to call that function. Since it returns nothing, you can't stick its non-existent return value into something else.
Second, print_type is a template function. You cannot call a template function without specifying the template parameters. So print_type() is ill-formed.
Third, even mpl::for_each<s>(print_type) doesn't work, because print_type is not a value (nor is it convertible to a value); it is a template. You cannot pass a template as the argument of a function. That's why visitors (for many things, not just MPL) are objects, which can have template operator() members.