Re: g++ template functions calling template functions.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 19, 2004 at 11:17:46AM -0800, David Sankel wrote:
> Hello all,

Hi David,
 
>   I am trying to port some msft c++ code to g++ 3.2.3 and it is generally
> going well except for this template error that I'm getting.
> 
>   I was unable to create a small example program that gives a compile error
> for the same situation.  

I'm almost certain you were just too lazy... ;-)

> It looks as though the compiler correctly resolves into the first
> version of this function (for maps).  When inside that function, the
> recursive fromDOMElement call doesn't correctly resolve to the pair
> version.  Why doesn't the compiler recognize this as the correct
> version?  It seems as though the first canidate in the error message
> matches the function call exactly.

You can't pass a temporary object to a function as a non-const
reference. (Btw, what is msft c++ and does it accept this code?)

,----[ Stroustrup: The C++ Programming Language 3rd; §7.2, 146f ]
| 
| A literal, a constant, and an argument that requires conversion can
| be passed as a const& argument, but not as a non-const argument.
| 
`----

So either define a variable explicitely and pass that to the function
if you need the function to modify it and you want to use it later or
change the functions to take a `const QDomElement&' as its first
argument.

I attached a small example with the code fixed. Please note, that this
is just a quick hack and that I needed to guess / improvise certain
things you didn't mention.

HTH
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux user                         - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \
#include <iostream>
#include <string>
#include <map>
#include <set>

using namespace std;

class QDomElement;

class QDomNode {
  public:
    bool isNull() {
	return true;
    } QDomElement toElement();
    QDomNode nextSibling() {
	return QDomNode();
    }
};

class QDomElement {
  public:
    bool isNull() {
	return false;
    } string tagName() {
	return "bar";
    }
    string attribute(const string & s1, const string & s2) {
	return "moooooo";
    }
    QDomNode firstChild() {
	return QDomNode();
    }
};

QDomElement QDomNode::toElement()
{
    return QDomElement();
}

template < typename T, typename U > void
fromDOMElement(QDomElement & e, std::map < T, U > *retval,
	       const std::string & type, const std::string & name)
{
    if (!e.isNull() &&
	(e.tagName() == type.c_str()) &&
	(e.attribute("name", "") == name.c_str())) {
	retval->clear();
	for (QDomNode i = e.firstChild(); !i.isNull(); i = i.nextSibling()) {
	    std::pair < T, U > element;
	    QDomElement arg = i.toElement();
	    fromDOMElement < T, U > (arg,
				     &element,
				     std::string("pair"), std::string(""));
	    (*retval)[element.first] = element.second;
	}
    }
}

template < class T > void
fromDOMElement(QDomElement & e, std::set < T > *retval,
	       const std::string & type, const std::string & name)
{
    if (!e.isNull() &&
	(e.tagName() == type.c_str()) &&
	(e.attribute("name", "") == name.c_str())) {
	retval->clear();
	for (QDomNode i = e.firstChild(); !i.isNull(); i = i.nextSibling()) {

	    T element;
	    QDomElement arg = i.toElement();
	    fromDOMElement(arg, &element, "element", "");
	    retval->insert(element);
	}
    }

}


template < typename T, typename U > void
fromDOMElement(QDomElement & e, std::pair < T, U > *retval,
	       const std::string & type, const std::string & name)
{

    if (!e.isNull() &&
	(e.tagName() == type.c_str()) &&
	(e.attribute("name", "") == name.c_str())) {
	QDomNode i = e.firstChild();
	if (i.isNull())
	    throw std::string("fromDOMElement with std::pair problems");
	QDomElement arg = i.toElement();
	fromDOMElement(arg, &retval->first, "first", "");
	i = i.nextSibling();
	if (i.isNull())
	    throw std::string("fromDOMElement with std::pair problems");
	arg = i.toElement();
	fromDOMElement(arg, &retval->second, "second", "");
    }
}


void
fromDOMElement(QDomElement & e, int *i,
	       const string & type, const string & name)
{
}


int main()
{
    QDomElement e;
    map < int, set < int > > m;
    const string f = "foo", b = "bar";

    fromDOMElement(e, &m, f, b);
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux