473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

combine templates with methods in different namespaces ?

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

Jul 23 '05 #1
9 1393

<th********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| 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
Jul 23 '05 #2
thanks,

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

Jul 23 '05 #3

"the.rubist" <th********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
| 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
Jul 23 '05 #4
my problem is that i can't change the third-party lib with namespaces.
namespace-templates would be a solution for me :)

Jul 23 '05 #5
th********@gmail.com wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #6
CrayzeeWulf wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #7
CrayzeeWulf wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #8
CrayzeeWulf wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #9
thank you, that is what i searched :)

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sam | last post by:
I would like to store html templates in a database. By using perl I would like to retrive the template ask the user to fill the template and store the whole file is template + the user data in a...
8
by: Bernd Fuhrmann | last post by:
Hi! I just tried to write a little program, but GCC (Mingw 3.3.1) refused to compile it. So I'd like to know if there's something wrong with my code or if it is a bug in GCC. ---snip---...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
11
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were...
3
by: SAL | last post by:
I’m fairly new to .NET. I’ve been developing in .NET for about 6 months. I have over 10 years experience with VB in general. Here’s what I have done so far: 1. Project is entirely .NET...
11
by: Mark | last post by:
Is that true you can't have a static method in VB.NET? Public Static Sub Initialize() -- ??? thanks Mark
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.