This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Dependency loop with templates


On Nov 15, 2006, at 9:30 AM, Ian Lance Taylor wrote:

Wai-chung Poon <tsdmilan@yahoo.com.hk> writes:

I encountered compilation problems with the following
code:

<< A.h >>
#include <vector>

namespace Ser
{
    template <class T>
    size_t StreamSize(const std::vector<T> &v)
    {
        size_t Result = 0;
        for (int i = 0; i < v.size(); ++i)
               Result += StreamSize(v[i]);
        return Result;
    }
}

<< B.h >>
#include "cx.h"  // defines CCX

namespace Ser
{
    size_t StreamSize(const CCX *p); // definition in
B.cpp
}

<< C.cpp >>
#include "A.h"    // Line (1)
#include "B.h"    // Line (2)

using namespace Ser;
using namespace std;

int main(int argc, char *argv[])
{
    vector<CCX *> v;
    size_t Size = StreamSize(v);
}

------

The returned error is "No matching function for call
to StreamSize(CCX *const&). If I swap line (1) with
line (2) in C.cpp, or remove the namespace definition
in B.cpp, the code compiles without any problem.
Besides, the above code used to work fine with gcc
2.96 but no longer works once I upgraded the gcc to
4.1.1.

If I don't swap the two lines in C.cpp, is there any
other way to resolve this? Thanks.


Names which are not dependent on template parameters, such as, in this
case, StreamSize, are looked up at template definition time, not at
template instantiation time.  This is known as two-phase lookup.

Sorry to be such a pest about this. I still don't understand this two phase lookup. Here is my logic:


StreamSize(v[i]) depends upon v[i] which depends upon std::vector<V> which depends upon V. So, StreamSize does (according to my logic) depend upon the template argument. Where did I go wrong?

Perry Smith
Ease Software, Inc.
pedz@easesoftware.com
http://www.easesoftware.com

Low cost SATA Products for IBMs p5, pSeries, and RS/6000 AIX systems




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]