Re: Dependency loop with templates

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

 



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

Wai-chung Poon <tsdmilan@xxxxxxxxxxxx> 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@xxxxxxxxxxxxxxxx
http://www.easesoftware.com

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




[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