473,387 Members | 1,573 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.

templates in parameters

I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:

MyType p = ConvFunc <RECT(rectangle);

when rectangle is of type RECT?
Aug 1 '08 #1
8 1223
ssylee wrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Post it here. Don't make people go to some unknown web site,
potentially full of who knows what...
Would I call the function like as follows:

MyType p = ConvFunc <RECT(rectangle);

when rectangle is of type RECT?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '08 #2
On Aug 1, 11:04*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
ssylee wrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).

Post it here. *Don't make people go to some unknown web site,
potentially full of who knows what...
Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sorry for the inconvenience. I was trying to make it easier to read
(as I don't know how to use code tags here [if i can use code tags
here]).

template <class T>
// MyType is a struct
MyType ConvFunc(T input)
{
// conversion manipulation that has nothing to do with the
conversion
}
Aug 1 '08 #3
On Aug 1, 1:53*pm, ssylee <staniga...@gmail.comwrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:

MyType p = ConvFunc <RECT(rectangle);

when rectangle is of type RECT?
The code is:

struct MyType { /*...*/ };

template <class T>
MyType ConvFunc(T input)
{
// ...
}

You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:

template<class Ret, class Arg>
Ret Conv( const Arg& arg )
{
// ...
}

void Foo( const int n )
{
MyType m1 = Conv( n ); // Error! Can't figure out return type
MyType m2 = Conv<MyType>( n ); // Ok
MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
// ...
}

Cheers! --M
Aug 1 '08 #4
On Aug 1, 11:14*am, mlimber <mlim...@gmail.comwrote:
On Aug 1, 1:53*pm, ssylee <staniga...@gmail.comwrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?

The code is:

*struct MyType { /*...*/ };

*template <class T>
*MyType ConvFunc(T input)
*{
* * // ...
*}

You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:

*template<class Ret, class Arg>
*Ret Conv( const Arg& arg )
*{
* *// ...
*}

*void Foo( const int n )
*{
* *MyType m1 = Conv( n ); *// Error! Can't figure out return type
* *MyType m2 = Conv<MyType>( n ); * * // Ok
* *MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
* *MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
* *// ...
*}

Cheers! --M
Thanks. I think you misunderstood what I'm trying to do. I'm trying to
convert the parameter (which could be in many different types) into
MyType. Would I need to use the power of templates to achieve the goal?
Aug 1 '08 #5
ssylee wrote:
On Aug 1, 11:04 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>ssylee wrote:
>>I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Post it here. Don't make people go to some unknown web site,
potentially full of who knows what...
>>Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Sorry for the inconvenience. I was trying to make it easier to read
(as I don't know how to use code tags here [if i can use code tags
here]).

template <class T>
// MyType is a struct
MyType ConvFunc(T input)
{
// conversion manipulation that has nothing to do with the
conversion
}
I am not sure what code tags are, sorry about making it inconvenient to
you. We post code here. Code is code, the alphabet is limited, the
numbers are common and the special symbols are in most cases don't need
to be tri- or di-graphed. Read the group and get the feel before
posting, it's never too late to learn the rules.

As for the function

template<class Tvoid foo(T t);

, you don't need to supply the template argument if your function
argument is already of that type, the compiler will take care of the
template argument deduction. If you do want to call a *different*
function with a particular argument, do supply it. If both are the
same, it won't matter. For, example, if I want to call my 'foo'
instantiated with 'unsigned' as its template argument, but supply an
integer, I would do

foo<unsigned>(42);

, for example. Since 42 is already an int, I don't have to write

foo<int>(42);

, IOW, it's the same as writing

foo(42); // compiler deduces T == int

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '08 #6
On Aug 1, 11:50*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
ssylee wrote:
On Aug 1, 11:04 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
ssylee wrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Post it here. *Don't make people go to some unknown web site,
potentially full of who knows what...
>Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sorry for the inconvenience. I was trying to make it easier to read
(as I don't know how to use code tags here [if i can use code tags
here]).
template <class T>
// MyType is a struct
MyType ConvFunc(T input)
{
* * * *// conversion manipulation that has nothing to do with the
conversion
}

I am not sure what code tags are, sorry about making it inconvenient to
you. *We post code here. *Code is code, the alphabet is limited, the
numbers are common and the special symbols are in most cases don't need
to be tri- or di-graphed. *Read the group and get the feel before
posting, it's never too late to learn the rules.

As for the function

* * *template<class Tvoid foo(T t);

, you don't need to supply the template argument if your function
argument is already of that type, the compiler will take care of the
template argument deduction. *If you do want to call a *different*
function with a particular argument, do supply it. *If both are the
same, it won't matter. *For, example, if I want to call my 'foo'
instantiated with 'unsigned' as its template argument, but supply an
integer, I would do

* * *foo<unsigned>(42);

, for example. *Since 42 is already an int, I don't have to write

* * *foo<int>(42);

, IOW, it's the same as writing

* * *foo(42); // compiler deduces T == int

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for the clarification Victor. I thought the parameters are
different types, but after taking a closer look, they are the same
type.
Aug 1 '08 #7
On Aug 1, 2:25*pm, ssylee <staniga...@gmail.comwrote:
On Aug 1, 11:14*am, mlimber <mlim...@gmail.comwrote:
On Aug 1, 1:53*pm, ssylee <staniga...@gmail.comwrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?
The code is:
*struct MyType { /*...*/ };
*template <class T>
*MyType ConvFunc(T input)
*{
* * // ...
*}
You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:
*template<class Ret, class Arg>
*Ret Conv( const Arg& arg )
*{
* *// ...
*}
*void Foo( const int n )
*{
* *MyType m1 = Conv( n ); *// Error! Can't figure out return type
* *MyType m2 = Conv<MyType>( n ); * * // Ok
* *MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
* *MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
* *// ...
*}

Thanks. I think you misunderstood what I'm trying to do. I'm trying to
convert the parameter (which could be in many different types) into
MyType. Would I need to use the power of templates to achieve the goal?
I don't believe I misunderstood you. I answered your question in the
sentence after the first code block and then attempted to demonstrate
a more general case where a function Conv() could convert between any
objects. Note also that you could use an overloaded cast operator to
do a conversion if the result type is a class:

#include <sstream>

class A
{
int i_;
public:
// ...

// Convert an A to a string automatically
operator std::string() const
{
std::ostringstream oss;
oss << i_;
}
};

void Foo( const A& a )
{
// These all do the same thing
std::string s1 = a; // implicitly calls A::operator std::string()
std::string s2 = static_cast<std::string>( a ); // Explicit
std::string s3 = Conv<std::string>( a ); // Use the func above
// ...
}

Cheers! --M
Aug 1 '08 #8
On Aug 1, 3:16*pm, mlimber <mlim...@gmail.comwrote:
On Aug 1, 2:25*pm, ssylee <staniga...@gmail.comwrote:
On Aug 1, 11:14*am, mlimber <mlim...@gmail.comwrote:
On Aug 1, 1:53*pm, ssylee <staniga...@gmail.comwrote:
I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:
MyType p = ConvFunc <RECT(rectangle);
when rectangle is of type RECT?
The code is:
*struct MyType { /*...*/ };
*template <class T>
*MyType ConvFunc(T input)
*{
* * // ...
*}
You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:
*template<class Ret, class Arg>
*Ret Conv( const Arg& arg )
*{
* *// ...
*}
*void Foo( const int n )
*{
* *MyType m1 = Conv( n ); *// Error! Can't figure out return type
* *MyType m2 = Conv<MyType>( n ); * * // Ok
* *MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
* *MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
* *// ...
*}
Thanks. I think you misunderstood what I'm trying to do. I'm trying to
convert the parameter (which could be in many different types) into
MyType. Would I need to use the power of templates to achieve the goal?

I don't believe I misunderstood you. I answered your question in the
sentence after the first code block and then attempted to demonstrate
a more general case where a function Conv() could convert between any
objects. Note also that you could use an overloaded cast operator to
do a conversion if the result type is a class:

*#include <sstream>

*class A
*{
* *int i_;
*public:
* *// ...

* *// Convert an A to a string automatically
* *operator std::string() const
* *{
* * *std::ostringstream oss;
* * *oss << i_;
Forgot:

return oss.str();
* *}
*};
Cheers! --M
Aug 1 '08 #9

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

Similar topics

1
by: Daryl | last post by:
Hello I am using apply-templates and would like to pass a parameter to the template using with-param. Using call-template passes the parameter, but when I use apply-templates, the parameter seems...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
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...
1
by: Amit | last post by:
Greetings. I am looking through some of the examples in Stroustrup's book and he has certain examples of doing compile time checks for a variety of things. One of them being the following... ...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
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: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
4
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.