473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template compilation problem

I'm having a problem compiling some template code. Here's a seemingly
useless block of code which I've stripped down to illustrate my problem.
Why is the second call to a.foo not accepted by the compiler? Advice
and suggestions are welcomed.

Thanks,
Mark

#include <iterator>
#include <list>

using namespace std;

template <typename T>
struct A
{
template <typename OutIter>
void foo (OutIter& oi);
};
template <typename T>
template <typename OutIter>
void A<T>::foo (OutIter& oi)
{}

int main ()
{
A<int> a;
list<int> output;

// this is fine:
back_insert_iterator<list<int> > bii = back_inserter(output);
a.foo(bii);

// this is not:
a.foo(back_inserter(output));
}
Mar 22 '06 #1
3 1503
> template <typename T>
struct A
{
template <typename OutIter>
void foo (OutIter& oi);
};
<snip>
a.foo(back_inserter(output));


The error has nothing to do with templates. The problem is that you
can't bind a temporary to a non-const reference (as I imagine your
compiler is telling you).
For example:
int &a = int(5);
is not valid, but
const int &a = int(5);
is OK. Your argument to foo is a non-const reference. Either pass it by
value, or do what you have done above.

Mar 22 '06 #2
Mark P wrote:
I'm having a problem compiling some template code. Here's a seemingly
useless block of code which I've stripped down to illustrate my problem.
Why is the second call to a.foo not accepted by the compiler? Advice
and suggestions are welcomed.
Hmm, I think I've figured this one out. The compiler doesn't like my
passing a temporary by non-constant reference. As you might guess, the
argument here is an Output Iterator and it suits me fine to modify and
discard the temporary, but I guess rules are rules.

Thanks,
Mark

#include <iterator>
#include <list>

using namespace std;

template <typename T>
struct A
{
template <typename OutIter>
void foo (OutIter& oi);
};
template <typename T>
template <typename OutIter>
void A<T>::foo (OutIter& oi)
{}

int main ()
{
A<int> a;
list<int> output;

// this is fine:
back_insert_iterator<list<int> > bii = back_inserter(output);
a.foo(bii);

// this is not:
a.foo(back_inserter(output));
}

Mar 22 '06 #3

Pete C wrote:
template <typename T>
struct A
{
template <typename OutIter>
void foo (OutIter& oi);
};
<snip>
a.foo(back_inserter(output));


The error has nothing to do with templates. The problem is that you
can't bind a temporary to a non-const reference (as I imagine your
compiler is telling you).
For example:
int &a = int(5);
is not valid, but
const int &a = int(5);
is OK. Your argument to foo is a non-const reference. Either pass it by
value, or do what you have done above.


is this related to the proposed C++ extension && on arguments?

Mar 23 '06 #4

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

Similar topics

2
11309
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
15
3060
by: iuweriur | last post by:
A few questions on the curiously recurring template pattern: This page: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate this part: template<typename T> struct ArithmeticType { T operator...
7
5933
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
3
1752
by: martin.druon | last post by:
Hi, I created a template class to represent hypermatrix. I would like to add methods where the number of parameters are checked during the compilation time. For example : template <size_t...
4
2717
by: Marcelo | last post by:
Hi everybody, This is my first time with the template class and I have an strange problem. I have spent all the afternoon trying to understand it, but I don't get the problem... I have three...
3
3482
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a way that could block the compilation if the...
1
3598
by: toton | last post by:
Hi, Is there any tool for template debugging, during compilation process ? Just like gdb or visual studio debugger do it during program execution, a step by step debug for the program, I am...
4
1798
by: carlos.urena.almagro | last post by:
Hello, I'm sorry if this post raises a question with obvious answer, or if it has been already posted and solved. I was wondering about how the compiler (in my case, gcc 4.0.2 on linux)...
8
7614
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
1
2643
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
7126
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,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.