How does the compiler interpret a template argument (reference vs. non-reference) ?

M

mrstephengross

I'd like to know if there's a standard way that the compiler interprets
a template argument. That is, let's say you pass a Foo instance as a
template argument to some function. Is the instance itself passed, or
does the compiler assume it's a Foo reference? For example:

================================
template<typename I> someFunc(I i) { /* ... */ }

Foo f;

someFunc(f);
=================================

In someFunc(), does I = Foo, or I = Foo&, or I = const Foo & ?

Thanks,
--Steve
 
M

mlimber

mrstephengross said:
I'd like to know if there's a standard way that the compiler interprets
a template argument. That is, let's say you pass a Foo instance as a
template argument to some function. Is the instance itself passed, or
does the compiler assume it's a Foo reference? For example:

================================
template<typename I> someFunc(I i) { /* ... */ }

Foo f;

someFunc(f);
=================================

In someFunc(), does I = Foo, or I = Foo&, or I = const Foo & ?

The first. You can make it the second or third by adding those
parameters to your template, e.g.,

template<typename I> someFunc(I& i) { /* ... */ }

Cheers! --M
 
M

mrstephengross

Can I also explicitly cast the Foo instance in the someFunc()
invocation?

For example: someFunc(const Foo &)f);

--Steve
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top