combine templates with methods in different namespaces ?

T

the.rubist

hi all

i have to components:
- a third-party (math) lib with namespaces
- a new lib with templates

i would merge this two components together. is there a way to customize
the namespace in a template ?

the code at the bottom is compilable. the results are:
10
10

but i would the following results:
10
0

i would configurable the namespace "Math" in the template. what's the
way to doing this ?

#include <iostream>
using namespace std;

namespace Single {
float compute( float a ) {
return a + a;
}
}

namespace Double {
double compute( double a ) {
return a - a;
}
}

namespace Math = Single;

template < typename T >
class Foo
{
public:
T call(T a) {
return Math::compute( a );
}
};

int main (int argc, char * const argv[]) {
{
namespace Math = Single;
Foo<float> f;
cout << f.call(5.0) << "\n";
}
{
namespace Math = Double;
Foo<double> f;
cout << f.call(5.0) << "\n";
}

}

regards,

the.rubist
 
C

Chris \( Val \)

| hi all
|
| i have to components:
| - a third-party (math) lib with namespaces
| - a new lib with templates
|
| i would merge this two components together. is there a way to customize
| the namespace in a template ?
|
| the code at the bottom is compilable. the results are:
| 10
| 10
|
| but i would the following results:
| 10
| 0
|
| i would configurable the namespace "Math" in the template. what's the
| way to doing this ?

[snip]

Sounds like template specialisation can help:

namespace Single {
float compute( float a ) {
return a + a;
}
}

namespace Double {
double compute( double a ) {
return a - a;
}
}

template<typename T>
class Foo
{
public:
T call( T a ) {
return Single::compute( a );
}
};

template<>
class Foo<double>
{
public:
double call( double a ) {
return Double::compute( a );
}
};

int main()
{
{
Foo<float> f;
cout << f.call(5.0) << "\n";
}
{
Foo<double> f;
cout << f.call(5.0) << "\n";
}

return 0;
}

Cheers,
Chris Val
 
T

the.rubist

thanks,

yes, i know, but then i must clone the code of every Single/Double -
method. but this is not what i want.
 
C

Chris \( Val \)

| thanks,
|
| yes, i know, but then i must clone the code of every Single/Double -
| method. but this is not what i want.

Well, you should then re-think your design.

I have to run now, but you might want to look at
a simple traits class to do what you want, whilst
pondering your design :)

Cheers,
Chris Val
 
T

the.rubist

my problem is that i can't change the third-party lib with namespaces.
namespace-templates would be a solution for me :)
 
C

CrayzeeWulf

i would merge this two components together. is there a way to customize
the namespace in a template ?

the code at the bottom is compilable. the results are:
10
10

but i would the following results:
10
0
Here is a possible solution. Assume that the file "OtherLib.h" declares all
the classes that you need to use from the library:


// OtherLib.h
#ifndef OtherLib_h
#define OtherLib_h

namespace Single {
float compute( float a ) {
return a + a;
}
} ;

namespace Double {
double compute( double a ) {
return a - a;
}
} ;

#endif

Put the following in a file "MyCommon.h":

// MyCommon.h
// :WARNING: Do not put any include guards in this file.

template<typename T>
class Foo
{
public:
T call(T a) {
return Math::compute(a) ;
}
} ;

Create two files "MyDouble.h" and "MySingle.h" that contain the following
respectively:

// MyDouble.h
#ifndef MyDouble_h
#define MyDouble_h

#include <OtherLib.h>

namespace MyDouble
{
namespace Math = Double ;
#include "MyCommon.h"
} ;

#endif

// MySingle.h
#ifndef MySingle_h
#define MySingle_h

#include <OtherLib.h>


namespace MySingle
{
namespace Math = Single ;
#include "MyCommon.h"
} ;

#endif

Now, you can do what you wanted in the file "main.cpp" that contains your
main():


// main.cpp
#include <iostream>
#include <MyDouble.h>
#include <MySingle.h>


int main (int argc, char * const argv[]) {
using namespace std;
{
namespace Math = MySingle ;
Math::Foo<float> f;
cout << f.call(5.0) << "\n";
}
{
namespace Math = MyDouble ;
Math::Foo<double> f;
cout << f.call(static_cast<double>(5.0)) << "\n";
}

}


Thanks,
 
C

CrayzeeWulf

CrayzeeWulf said:
cout << f.call(static_cast<double>(5.0)) << "\n";
You do not need the "static_cast<double>" there. Replace that with:

cout << f.call(static_cast<double>(5.0)) << "\n

Thanks,
 
C

CrayzeeWulf

CrayzeeWulf said:
cout << f.call(static_cast<double>(5.0)) << "\n";
You do not need the "static_cast<double>" there. Replace that with:

cout << f.call(static_cast<double>(5.0)) << "\n" ;

Thanks,
 
C

CrayzeeWulf

CrayzeeWulf said:
cout << f.call(static_cast<double>(5.0)) << "\n";
Oh well. I hope superseding articles works well. You do not need the
"static_cast<double>" there. Replace that with:

cout << f.call(5.0) << "\n" ;

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top