473,386 Members | 1,817 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.

How to deal with template references?


Hi.

What happens if a given template typename T is int& and you need to
reference an int pointer, or even an int?

e.g:

template <typename T>
void do_something( void *ptr, void(*funct)(T arg1)) {
funct( *(T *)ptr);
}

it's ok if you call it

void int_ok( int a);
int global = 123;
do_something( &global, int_ok);

but not ok if you call

void int_not_ok( int& a);
do_something( &global, int_not_ok);

if you don't use the templates, the logic is all ok, because a content
of a pointer can be referenced to a calling function.

by the way, the error caught is that you cannot reference a pointer to
a reference, like *(int& *). To my common sense, this is just like
*(int *).

Is there any obscure method to remove a reference from a template? :)

Nov 20 '06 #1
7 1327
1. I do not understand, why are you using void* instead of

template <typename T>
void do_something( T* ptr, void(*funct)(T arg1)) {
funct( *ptr);
}

2. If you are writing template parametrized with type T and you want to
have type U which is defined as
(a) if T is reference to X, U is X
(b) in any other case U is T
You can write it this way:

// Reference removal code
template<typename T>
struct RemoveRef
{
typedef T Type;
};

template<typename T>
struct RemoveRef<T&>
{
typedef T Type;
};

// Now you can use it
template<typename T>
struct HereINeedParameterWithoutReference
{
typedef
typename RemoveRef<T>::Type TWithoutReference;
};

3. An now forget previous rows. You need to change the template this
way and you have all you need:
template <typename T, typename F>
void do_something(T* ptr, F& funct)
{
funct(*ptr);
}

Nov 20 '06 #2
gu************@gmail.com wrote:
Hi.

What happens if a given template typename T is int& and you need to
reference an int pointer, or even an int?

e.g:

template <typename T>
void do_something( void *ptr, void(*funct)(T arg1)) {
funct( *(T *)ptr);
}

it's ok if you call it

void int_ok( int a);
int global = 123;
do_something( &global, int_ok);

but not ok if you call

void int_not_ok( int& a);
do_something( &global, int_not_ok);

if you don't use the templates, the logic is all ok, because a content
of a pointer can be referenced to a calling function.

by the way, the error caught is that you cannot reference a pointer to
a reference, like *(int& *). To my common sense, this is just like
*(int *).

Is there any obscure method to remove a reference from a template? :)
It's not so obscure if you think about it. I think this should work:

template<class Tstruct deReference {
typedef T type;
}
template<class Tstruct deReference<T&{
typedef typename deReference<T>::type type;
// and since there are no references to references, you might even do
// typedef T type;
// here, I guess
}

template <typename T>
void do_something( void *ptr, void(*funct)(T arg1)) {
funct( *(typename deReference<T>::type *)ptr);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '06 #3
ondra.holub wrote:
[..forgotten..]
3. An now forget previous rows. You need to change the template this
way and you have all you need:
template <typename T, typename F>
void do_something(T* ptr, F& funct)
{
funct(*ptr);
}
Why 'F&'? Shouldn't it be just 'F funct'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '06 #4
Why 'F&'? Shouldn't it be just 'F funct'?

Well, it should be 'const F&'. For ordinary function it may be F only,
but const F& allows passing simple functor:

template <typename T, typename F>
void do_something(T* ptr, const F& funct)
{
funct(*ptr);
}

class Fn
{
public:
Fn() { }

void operator()(double prm) const
{
std::cout << "Fn::operator()(" << prm << ")\n";
}
};

int main()
{
int global = 123;
do_something( &global, Fn());
}

Nov 20 '06 #5
ondra.holub wrote:
>Why 'F&'? Shouldn't it be just 'F funct'?

Well, it should be 'const F&'. For ordinary function it may be F only,
but const F& allows passing simple functor:
[..classical example redacted..]
Are you saying that having 'F funct' instead of 'F const& funct' does
not allow passing "simple functor"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '06 #6
No. Of course it allows (at least in case that copying is allowed). I
only wanted to pass instance of class (functor) in better way.

I think that the example would need to use better mechanism to select
best way to pass parameter (class instance with reference, basic types
with value), but such code would hide the answer to first question.

Nov 20 '06 #7
Ondra,

Your RemoveRef typedef solved the problem. Congratulations! :)

1. I need to deference because I have the following code:
class BaseType {
protected:
const char *mName;
public:
BaseType() : mName(NULL) {}
BaseType( const BaseType& vrBaseType) : mName( NULL) {}

virtual void * GetAddress() const = 0;
virtual size_t GetSize() const = 0;
const char *GetName() const { return mName; }
};

template <typename T>
class VariantType : BaseType {
private:
const T& mItem;
public:
VariantType( const T& vItem) : mItem(vItem), BaseType() { mName =
typeid(vItem).name(); }
void* GetAddress() const { return (void *)&mItem; }
size_t GetSize() const { return sizeof(mItem); }
};
All I have access is BaseType, and it's type (because of template
completion).

template <class T_INSTANCE, typename T_FUNCTION >
class EventHandler : public IEventHandler {
protected:
T_INSTANCE* mpInstance;
T_FUNCTION mpFunction;
public:
EventHandler( T_INSTANCE* vpInstance, T_FUNCTION vpFunction):
mpInstance( vpInstance), mpFunction( vpFunction) {
}
void Execute( ParamHelper& vrHelper) {
vrHelper.Call( mpInstance, mpFunction);
}
};

the PAramHelper Call:

template <class T_INSTANCE, class A1>
void Call( T_INSTANCE* vpInstance,
void (T_INSTANCE::*vpFunction)(A1)) {
AssertType<A1>( 0);
(vpInstance->*vpFunction)(*(RemoveRef<A1>::Type
*)mArguments[0]->GetAddress()); }
And ParamHelper was previously parsed:

template<class A1>
void Parse( A1& arg1) {
Reset();
mArguments.Append() = (BaseType *) new VariantType<A1>( arg1);
}
Thank you all!

ondra.holub wrote:
1. I do not understand, why are you using void* instead of

template <typename T>
void do_something( T* ptr, void(*funct)(T arg1)) {
funct( *ptr);
}

2. If you are writing template parametrized with type T and you want to
have type U which is defined as
(a) if T is reference to X, U is X
(b) in any other case U is T
You can write it this way:

// Reference removal code
template<typename T>
struct RemoveRef
{
typedef T Type;
};

template<typename T>
struct RemoveRef<T&>
{
typedef T Type;
};

// Now you can use it
template<typename T>
struct HereINeedParameterWithoutReference
{
typedef
typename RemoveRef<T>::Type TWithoutReference;
};

3. An now forget previous rows. You need to change the template this
way and you have all you need:
template <typename T, typename F>
void do_something(T* ptr, F& funct)
{
funct(*ptr);
}
Nov 20 '06 #8

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

Similar topics

7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
3
by: Tony Johansson | last post by:
Hello experts! Is there any limitation for template parameter that normal function parameters doesn't have? What does this text below mean. To use template parameters as constans is normally...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
3
by: a | last post by:
Hi, I'm trying to create a helper class that will allow me to use a pointer or reference to a class without knowing if it is a pointer or reference. Conceptual example: //******************...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
8
by: Imre | last post by:
Hi I'm looking for a way to make sure that whenever a new instance of a class template A is created, then an instance of class template B is also created, with the same template parameters. Of...
6
by: desktop | last post by:
I am trying to understand the following template: template <typename T> inline T const& max (T const& a, T const& b) { return a < b ? b : a; } does it say that the function max returns a...
1
by: subramanian100in | last post by:
consider template<typename TTest { // ... }; We can have a pointer type as argument to a template class. For example, we can have, int x = 100; Test<int*obj(&x); // assuming a suitable ctor...
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: 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: 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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.