Re: Dependency loop with templates

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

 



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.

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

Ian

[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