473,387 Members | 2,436 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,387 software developers and data experts.

setting default value for template parameter list

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,

Jul 23 '05 #1
6 1812
bl**********@gmail.com wrote:
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<double>?

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);
}

Jul 23 '05 #2
foo and bar are both member functions of a class. Will the above syntax
still work?

Jul 23 '05 #3
bl**********@gmail.com wrote:
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
Jul 23 '05 #4


Victor Bazarov wrote:
bl**********@gmail.com wrote:
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

Jul 23 '05 #5
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>::operator *(const Matrix<T>& B)
{
double alpha=1;
....
}

template<>
Matrix<std::complex<float> > Matrix<std::complex<float> >::operator *(
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<std::complex<float>
::operator*(Matrix<std::complex<float> > const&)


Any idea???

Jul 23 '05 #6
Victor Bazarov wrote:
bl**********@gmail.com wrote:
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.

Jul 23 '05 #7

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

Similar topics

26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
11
by: BRG | last post by:
I know that default template arguments cannot be used in function templates but are default function parameters legal? That is, is this: ---------------------------------- #include <functional>...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
11
by: Matthias Pfeifer | last post by:
Hi there, I am trying to declare a function that takes a std::list<parameter. I want this function to have an empty list as a default parameter. It's a template function also. Currently i am...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.