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


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.

The simple fix here would be to have A.h include B.h.

Ian


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