Connecting Tech Pros Worldwide Forums | Help | Site Map

Returning a template argument

eriwik@student.chalmers.se
Guest
 
Posts: n/a
#1: Dec 8 '06
Consider this:

#include <iostream>

template<int A>
struct test {
const int& get() { return A; }
}

int main()
{
test<4t;
std::cout << t.get();
}

1. Is it legal (it compiles under VC++8).
2. Is it a good idea? At first I thought of having a const int as a
member of the class and initialize it to A, but then I realized that I
might not have to, I'm just not sure if it's a good idea or not.

--
Erik Wikström


peter koch
Guest
 
Posts: n/a
#2: Dec 8 '06

re: Returning a template argument



eriwik@student.chalmers.se skrev:
Quote:
Consider this:
>
#include <iostream>
>
template<int A>
struct test {
const int& get() { return A; }
}
>
int main()
{
test<4t;
std::cout << t.get();
}
>
1. Is it legal (it compiles under VC++8).
No. Technically f is legal but using it is not. The reason is that you
are returning a reference to a local variable and has nothing to do
with using templates.

Apart from that, I believe that while the program might compile most
compilers will give you a warning (including VC++8). Ignoring warnings
could be a very bad idea.
Quote:
2. Is it a good idea? At first I thought of having a const int as a
member of the class and initialize it to A, but then I realized that I
might not have to, I'm just not sure if it's a good idea or not.
A good idea? I do not really see the purpose of your code. Also, I do
not see why you do not simply return int.

/Peter

Victor Bazarov
Guest
 
Posts: n/a
#3: Dec 8 '06

re: Returning a template argument


eriwik@student.chalmers.se wrote:
Quote:
Consider this:
>
#include <iostream>
>
template<int A>
struct test {
const int& get() { return A; }
}
>
int main()
{
test<4t;
std::cout << t.get();
}
>
1. Is it legal (it compiles under VC++8).
2. Is it a good idea? At first I thought of having a const int as a
member of the class and initialize it to A, but then I realized that I
might not have to, I'm just not sure if it's a good idea or not.
It is legal. The temporary reference is returned to a temporary 'int'
object. The reference only survives until the end of the full expression
in which the call happens (in your case until the semicolon in the last
statement in 'main'), the temporary 'int' survives until the reference to
it goes away. Just don't do

int const & ri = t.get();

because right after that statement 'ri' is invalid.

Now, whether it's a good idea I am not sure. Why not simply return
a value instead of a reference?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


John Carson
Guest
 
Posts: n/a
#4: Dec 8 '06

re: Returning a template argument


<eriwik@student.chalmers.sewrote in message
news:1165583518.360940.63370@16g2000cwy.googlegrou ps.com
Quote:
Consider this:
>
#include <iostream>
>
template<int A>
struct test {
const int& get() { return A; }
}
You omitted the closing semi-colon.
Quote:
int main()
{
test<4t;
std::cout << t.get();
}
>
1. Is it legal (it compiles under VC++8).
Yes.
Quote:
2. Is it a good idea? At first I thought of having a const int as a
member of the class and initialize it to A, but then I realized that I
might not have to, I'm just not sure if it's a good idea or not.
Difficult to say without more context. In the example you have given, the
function becomes

const int& get() { return 4; }

Since a reference requires a variable with an address, the program must
create a temporary int with a value of 4 and return a reference to that.
This isn't normally a great idea. Why do you want to do it? Just because you
can?

Your code is similar in effect to:

template<int A>
struct test
{
const int& get()
{
int x = A;
return x;
}
};


--
John Carson


Victor Bazarov
Guest
 
Posts: n/a
#5: Dec 8 '06

re: Returning a template argument


peter koch wrote:
Quote:
eriwik@student.chalmers.se skrev:
Quote:
>Consider this:
>>
>#include <iostream>
>>
>template<int A>
>struct test {
> const int& get() { return A; }
>}
>>
>int main()
>{
> test<4t;
> std::cout << t.get();
>}
>>
>1. Is it legal (it compiles under VC++8).
No. Technically f is legal but using it is not. The reason is that you
are returning a reference to a local variable and has nothing to do
with using templates.
What local variable? It's a temporary.
Quote:
Apart from that, I believe that while the program might compile most
compilers will give you a warning (including VC++8). Ignoring warnings
could be a very bad idea.
>
Quote:
>2. Is it a good idea? At first I thought of having a const int as a
>member of the class and initialize it to A, but then I realized that
>I might not have to, I'm just not sure if it's a good idea or not.
>
A good idea? I do not really see the purpose of your code. Also, I do
not see why you do not simply return int.
That's the right way (to return 'int' in that case), and the purpose is
to save memory per instance of 'test' class (no need to keep a data
member). However, it has an adverse effect: in this case

test<5t5;
test<4t4;

the objects have different types, whereas in this case

test t5(5);
test t4(4);

they are of the same type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


David Harmon
Guest
 
Posts: n/a
#6: Dec 8 '06

re: Returning a template argument


On 8 Dec 2006 05:11:58 -0800 in comp.lang.c++,
"eriwik@student.chalmers.se" <eriwik@student.chalmers.sewrote,
Quote:
>2. Is it a good idea? At first I thought of having a const int as a
>member of the class and initialize it to A,
What are you trying to accomplish? I guess that a const member, rather
than a proliferation of an integral number of separate template
instantiation types, will ultimately be a lot less hairy. Unless this
is part of some metaprogramming scheme.

As others have noted, return the int by value, not by reference.

Closed Thread