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

g++ "no matching function to call" using a template

Hello,

My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3.
(the other system is Windows XP with MS Visual Studio .NET 2003)

I have an auto_array<T> template (based on a template taken from the
Corona
project hosted at SourceForge) which basically wants to implement
std::auto_ptr<T>
semantics for an array.

It used to compile and run the test program I wrote for it but suddenly
it stopped
compiling and I have no idea what did I do wrong (or change at all).

On MS Visual Studio .NET 2003 the sample test program below (which I
clipped
down to demo the problem) compiles and runs find.

Here is the sample program:

#include <cstddef>
#include <cstring>

template<typename T>
class auto_array
{
public:
explicit auto_array (T* initial = 0) : array(initial) { }
auto_array(auto_array<T>& other) : array(other.release()) { }
~auto_array() { delete[] array; }
T* get() const { return array; }
T* release() { T* old = array; array = 0; return old; }
void reset(T* a = 0)
{
if (a != array) { delete[] array; array = a; }
}
void reset(auto_array<T>& a)
{
if (this != &a) { delete[] array; array = a.release(); }
}
auto_array<T> &operator= (T* a) { reset(a); return *this; }
auto_array<T> &operator= (auto_array<T>& a)
{
reset(a.release()); return *this;
}
private:
T* array;
};

auto_array<char> f(auto_array<char>& input) { return input; }

int main(int argc, char* argv[])
{
auto_array<char> a,b,c;
c.reset(a);
b = c;
// next two lines causes the errors
c.reset(f(b));
b = f(b);
}

The lines which cause the errors are the last two in main().

Could someone please tell where is my mistake? I can't find it myself
from digging the net or reading the books.

Thanks,

--Amos

Jul 23 '05 #1
7 5057

"Vaca Louca" <am**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hello,
|
| My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3.
| (the other system is Windows XP with MS Visual Studio .NET 2003)
|
| I have an auto_array<T> template (based on a template taken from the
| Corona
| project hosted at SourceForge) which basically wants to implement
| std::auto_ptr<T>
| semantics for an array.
|
| It used to compile and run the test program I wrote for it but suddenly
| it stopped
| compiling and I have no idea what did I do wrong (or change at all).

[snip]

I'm not sure whay you need this function, but instead of it...

| auto_array<char> f(auto_array<char>& input) { return input; }

[snip]

....change it to us a reference, and possibly even make it
a template function as well:

template<typename T>
auto_array<T>& f( auto_array<T>& input )
{
return input;
}

Cheers,
Chris Val
Jul 23 '05 #2
This function is used to test the function parameter passing and
return of a local instance of auto_array<T>, and to make sure the value
is copied correctly (and ownership is given up correctly).

Actually, the original test function is:

auto_array<char> f(auto_array<char>& input)
{
auto_array<char> local(input);
return local;
}

Since the function returns a local variable it can't return a
reference,
but must use pass-byt-value.
I just shortened the code as much as possible while still trying to
demonstrate the problem.

Thanks,

--A

Jul 23 '05 #3
Replying to myself (but maybe of benefit to others):

Apparently, the function return value (the one being return by f()
in my example) is considered "const", and therefore the operator=
and the copy constructor were not considered as candidates which
will accept this as an argument.
Once I made them accept const parameters (and override the
const using const_cast<> in order to actually reset the parameter's
values)the program passed compilation.

Here is the fixed program:

template<typename T>
class auto_array
{
public:
explicit auto_array (T* initial = 0) : array(initial) { }
auto_array(const auto_array<T>& a) : array(a.array)
{
if (this != &a)
{
const_cast<auto_array<T>*>(&a)->array = 0;
}
}
~auto_array() { delete[] array; }
T* get() const { return array; }
T* release() { T* old = array; array = 0; return old; }
void reset(T* a = 0)
{
if (a != array) { delete[] array; array = a; }
}
void reset(const auto_array<T>& a)
{
if (array != a.array) {
delete[] array;
array = const_cast<auto_array<T>*>(&a)->release();
}
}
auto_array<T> &operator= (T* a) { reset(a); return *this; }
auto_array<T>& operator= (const auto_array<T>& a)
{
reset(const_cast<auto_array<T>*>(&a)->release()); return *this;
}
private:
T* array;
};

auto_array<char> f(auto_array<char>& input) { return input; }

int main(int argc, char* argv[])
{
auto_array<char> a,b,c;
b.reset(a);
c = b;
// next lines used to cause the errors
c.reset(f(b)); // simple reset
c = f(b); // operator=
auto_array<char> d(f(c)); // copy constructor
}

I only wonder if people can comment whether this looks to them
as a good code. I'm a bit uneasy with all this const overriding
(I think I should be worried at the design level - laying to the
template's users about the constness of their objects).

Thanks,

--Amos

Jul 23 '05 #4

"Vaca Louca" <am**********@gmail.com> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
| This function is used to test the function parameter passing and
| return of a local instance of auto_array<T>, and to make sure the value
| is copied correctly (and ownership is given up correctly).
|
| Actually, the original test function is:
|
| auto_array<char> f(auto_array<char>& input)
| {
| auto_array<char> local(input);
| return local;
| }
|
| Since the function returns a local variable it can't return a
| reference, but must use pass-byt-value.

True, but you failed to mention that :-)

| I just shortened the code as much as possible while still trying to
| demonstrate the problem.

Why not make the local instance 'static' ?

auto_array<char>& f( auto_array<char>& input )
{
static auto_array<char> local( input );
return local;
}

Then you can return by reference, and you do not need
to go breaking things with const_cast as you have in
your other post :-)

Btw, where do you allocate memory ?

Cheers,
Chris Val
Jul 23 '05 #5
I found a rather easy hack into your problem ... just add this line as
a public member function:

operator auto_array<T>&(){return *this;}

It works with no complaints whatsoever!! (at least for gcc)

Hold it! I still haven't got a clue as to what's happenning here. When
returning an object by value, we do need a copy constructor, don't we?
But array_ptr hasn't got one! It even works if I declare a copy-ctor
myself and keep it in the *private* section! After experimenting a
little with this myself, I'm beginning to suspect that the move-ctor
symantecs have already been implemented in gcc (or I have been living
in the stone age!)

And as for the "no matching function" problem, the thing is gcc is a
little lazy when it comes to matching up template parameters - a ctor
needs a parameter of const T*, and it complains when it sees a call
passed with a simple T* - it doesn't see the match without a partial
specialization or something.
Apparently, the function return value (the one being return by f()
in my example) is considered "const",


I wouldn't agree - you might want to look up one of my recent threads
and what others had to say about it:

http://groups-beta.google.com/group/...2943e5798f58db

Samee

Jul 23 '05 #6
I'm not sure what's happens with your operator overload either.

I assume by "array_ptr" you mean "auto_array"? It does have
a copy constructor.

As far as I know the language, a "const T" should accept a plain
"T", though not the toher way around.

I see the thread you are reffering to. Don't know what to say about it.

Thanks,

--Amos

Jul 23 '05 #7
STATIC??
It's not thread-safe to begin with, and I need to allocate variable
array size on top of this.
Memory allocation is done with "new char[buffer_size]" and is
used here just because Visual C++ 2003 .NET still doesn't
support local arrays with variable size, e.g.:

#ifdef _MSC_VER
auto_array<char> auto_key_buffer(new char[key_buffer_size]);
char *key_buffer(auto_key_buffer.get());
#else
char key_buffer[key_buffer_size];
#endif

I need this because I don't want to leak memory in case of an exception
or other unplanned function return.

Cheers,

--Amos

Jul 23 '05 #8

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

Similar topics

10
by: Lionel B | last post by:
Greetings, I cannot figure out why the following code does not compile: --- BEGIN CODE --- typedef double ftype(double); struct A {
0
by: H. S. | last post by:
Hi, I am trying to compile the examples in arpack++ library on x86 Linux (flavor of Red Hat) running 2.4.24 kernel and g++ (3.2 20020903 version). Here is part of the compiler error I am...
2
by: Lateralus | last post by:
headers/vector3.h: In member function ‘Vector3 Vector3::operator*(Scalar)’: headers/vector3.h:13: error: no matching function for call to ‘Vector3::Vector3(Vector3)’...
28
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last...
3
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates...
3
by: Aries Sun | last post by:
I am reading the book "Effective C++" Item 25 mentioned the following code: // a first cut at a class yielding NULL pointer objects class NullClass { public: template<class T ...
2
by: Jolie Chen | last post by:
I am learning template programming now, and I wrote the following code #include <iostream> #include "Queue.h" using namespace std; template <typename Tclass QueueItem { public: QueueItem(T...
0
by: Peter | last post by:
Hi I'm stuck and would appreciate some help. I have created a Class library project that contains a number of interface definitions that must be used by a range of other VB.NET applications. ...
3
by: Imaginativeone | last post by:
XML <nodeAA>AA</nodeAA> <nodeBB>BB</nodeBB> <nodeCC> <From>12/05</From> <To>11/06</To> <Months>12</Months> <Amount>10.00</Amount> ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
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,...
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.