Use different sets of functions based on template parameters (C++ traits?)?

Include #include namespace xxx { class X { }; X sin(X) { return X(); } } //xxx std::ostream& operator void use_sin(T t) { using std::sin; //primitive types are not in a namespace, //and with some implementation sin(double) etc might not be available //in global namespace std::cout.

So you're suggesting I bunch up the math functions for each set (double, BigNum, ..) and put each in one namespace. Then, in the place where I'll want to have argument dependent lookup, I'll expose them all with using namespace xxx; declarations? This is pretty much how it is right now.

Can't I "assign" namespaces like I have shown in the question? What's the closest equivalent to that in C++? With the correct namespace "stored" in some "variable" math (see question) I can always use math::sin and be sure that the correct function is being called.

– chillu Aug 7 '10 at 19:54 @cillu: The point is: if the argument type is in a namespace, then the same namespace will be searched for a suitable function to call. When the compiler sees xxx::X x; sin(x);, it will look into the global namespace and namespace xxx to find a suitable function to call. - I think you can't assign namespaces like that, although this doesn't mean there couldn't be other solutions.

- Namespaces are pain. IMO people should just place their overloads of standard functions in the std namespace (as far as the rules - which could be relaxed - allow) :) – UncleBens Aug 7 '10 at 19:59 @UncleBens: I'm completely fine by letting the compiler doing the lookup. There's only one problem I see: Whenever I use a new type MyType I'll have to come back to this file and expose the math functions for the type T in namespace xxx.

I'd really like to not touch the template headers everytime for changes that are being effected by the client! – chillu Aug 7 '10 at 20:08 @cillu: The above should work in all cases, if T's corresponding sin function is either in the global namespace, in the std namespace or in the same namespace as T itself. There shouldn't be any need to modify anything.In the example, the correct overload for T=X is picked automatically without any change to use_sin.

– UncleBens Aug 7 '10 at 20:12 @chillu: No need to do that. If the class or struct is in namespace ns, the compiler will automatically search namespace ns when resolving sin(x). The client just has to ensure his libraries are #include-d before he first uses your function.

The only case when you need using namespace in your library is when the operations are in a different namespace eg. Float vs. std::sin. – jpalecek Aug 7 '10 at 20:15.

I don't want to (re! )define what it means to take sine of doubles and floats! I want to use appropriate (existing) library functions within my class depending on the template parameter (double or BigNum or ..) – chillu Aug 7 '10 at 19:27.

I'm including this answer because I finally managed to get what I want (with help from very nice folk on ##c++ at irc.freenode. Net). This method enables both ADL as well as a static set of places (xxx::math) to look in for the definition of the math functions.

This way, if the type parameter T of the class Test is such that: if T defines math functions as members then we can use ADL and not touch (add to) the namespace xxx::math. If T doesn't define math functions as members but uses functions from a specific namespace then we can add to the namespace xxx::math like in the example below. Library looks like this: #include #include namespace xxx { // include the usual math namespace math { using std::asin; } template class Test { std::vector array; public: Test(const typename std::vector::size_type length) { assert(length >= 1); array.

Assign(length, T(0.0)); } friend std::ostream& operator btest(3); cout.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions