[cfe-dev] A question about template class forwarding definition

David Blaikie dblaikie at gmail.com
Tue Sep 9 08:34:26 PDT 2014


On Tue, Sep 9, 2014 at 12:00 AM, Jiangning Liu <liujiangning1 at gmail.com>
wrote:

> Hi,
>
> I have a class forwarding definition issue. For the following small test
> case, gcc can build pass, but clang(trunk) will build fail.
>
> ====================================
>
> template<class T> class B;
>
> template<class T>
> class A
> {
>   public:
>     void f(void) const { B<double> e; g(e); }
>     void g(B<double> &e) const;
> };
>
> template<class T>
> class B
> {
> public:
>   T x[9];
>
>   B() {
>     for(int i=0;i<9;i++) x[i]=0;
>   }
> };
> $ g++ -std=c++03 -c tt.cc
> $ clang++ -std=c++03 -c tt.cc
> tt.cc:7:36: error: implicit instantiation of undefined template 'B<double>'
>     void f(void) const { B<double> e; g(e); }
>                                    ^
> tt.cc:1:25: note: template is declared here
> template<class T> class B;
>                         ^
> 1 error generated.
>
> ====================================
>
> Is this bug in clang?
>
> If not, how should I write the forwarding declaration for class B, and why
> gcc can pass?
>

This is one of those cases where a compiler isn't required to diagnose the
fact that your program is invalid (the standard wording is "invalid, no
diagnostic required").

The particular issue is that B<double> is not a dependent name - so the
compiler can resolve it at template (A<T>) declaration time, without having
to wait for an instantiation to realize that B<double> is not defined.

That's my understanding at least.


>
> Thanks,
> -Jiangning
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140909/33ba954f/attachment.html>


More information about the cfe-dev mailing list