setting default value for template parameter list

B

bluekite2000

I want to accomplish the following

void foo(const T &v1, const T &v2)
{
if(v1&v2 is of type double)
double alpha=1.0;
else if (v1&v2 is of type complex double)
complex double alpha=(1.0,1.0);

bar(v1,v2,alpha);
}

void bar(const T&v1, const T&v2, const T& alpha)
{
}

but dont know how. Please help
Regards,
 
R

Rolf Magnus

I want to accomplish the following

void foo(const T &v1, const T &v2)
{
if(v1&v2 is of type double)

?? Is foo supposed to be a template?
double alpha=1.0;
else if (v1&v2 is of type complex double)
complex double alpha=(1.0,1.0);

Did you mean std::complex said:
bar(v1,v2,alpha);
}

What if v1 and v2 are of neither of those 2 types? Anyway, maybe you are
searching for specialization:

template <typename T>
void foo(const T &v1, const T &v2)
{
//whatever you want the default to be
}

template<>
void foo(const double &v1, const double &v2)
{
const double alpha(1.0);
bar(v1,v2,alpha);
}

template<>
void foo(const std::complex<double> &v1, const std::complex<double> &v2)
{
const std::complex<double> alpha(1.0,1.0);
bar(v1,v2,alpha);
}
 
B

bluekite2000

foo and bar are both member functions of a class. Will the above syntax
still work?
 
V

Victor Bazarov

foo and bar are both member functions of a class. Will the above syntax
still work?

Which above syntax? I don't see anything _above_ what you wrote.
[Shit, I begin to sound like Brian]

Yes, the full specialisation of a template member is possible, you just
need to define it outside the class definition.

V
 
D

Default User

Victor said:
foo and bar are both member functions of a class. Will the above syntax
still work?

Which above syntax? I don't see anything _above_ what you wrote.
[Shit, I begin to sound like Brian]


It's a GOOD thing.




Brian
 
B

bluekite2000

I tried the full specialization as suggested
template <typename T>
class Matrix
{
....
Matrix operator * (const Matrix<T>& B);
....
};

template<typename T>
Matrix<T> Matrix<T>::eek:perator *(const Matrix<T>& B)
{
double alpha=1;
....
}

template<>
Matrix<std::complex<float> > Matrix<std::complex<float> >::eek:perator *(
const Matrix<std::complex<float> >& B)
{
std::complex<float> alpha=std::complex<float>(1,0);
....
}

but received an error from gcc
multiple definition of `Matrix said:
::eek:perator*(Matrix<std::complex<float> > const&)

Any idea???
 
R

Rolf Magnus

Victor said:
foo and bar are both member functions of a class. Will the above syntax
still work?

Which above syntax? I don't see anything _above_ what you wrote.
[Shit, I begin to sound like Brian]

Yes, the full specialisation of a template member is possible, you just
need to define it outside the class definition.

Since when? I thought if you want to specialize a member, you have to
specialize the whole class.
 

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,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top