[cfe-dev] Clang++-3.7 compilation error with CRTP code example

Reid Kleckner via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 2 16:04:36 PST 2016


DE is not complete while you instantiate the class body of Parent. Using
sizeof gives a better diagnostic:

template <typename DE>
struct Parent
{
    static const int value = sizeof(DE);
...
t.cpp:6:30: error: invalid application of 'sizeof' to an incomplete type
'Child<int, 4>'
    static const int value = sizeof(DE);
                             ^~~~~~~~~~
t.cpp:14:16: note: in instantiation of template class 'Parent<Child<int, 4>
>' requested here
struct Child : Parent< Child<T,N> >
               ^
t.cpp:22:18: note: in instantiation of template class 'Child<int, 4>'
requested here
    Child<int,4> c;
                 ^
t.cpp:14:8: note: definition of 'Child<int, 4>' is not complete until the
closing '}'
struct Child : Parent< Child<T,N> >
       ^

On Wed, Mar 2, 2016 at 2:08 PM, Diptorup Deb via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi,
>
> In the below code I am trying to use CRTP to use the static member "value"
> from the Child class in the Parent class. When compiling the code with g++
> 5.2.1 with the "-pedantic" flag, I am able to compile without errors or
> warning, and on execution both c.print_value(); and
> Child<int,4>::print_value(); print out 4.
>
> ------------------------------------------
> #include <iostream>
>
> template <typename DE>
> struct Parent
> {
>     static const int value = DE::value;
>     static void print_value ()
>     {
>         std::cout << "Value : " << value << '\n';
>     }
> };
>
> template <typename T, int N>
> struct Child : Parent< Child<T,N> >
> {
>     static const int value = N;
> };
>
> int
> main ()
> {
>     Child<int,4> c;
>     c.print_value();
>     Child<int,4>::print_value();
> }
> ------------------------------------------
>
> However when compiling the same code with clang++3.7, I encounter
> compilation failures.
>
> crtp_clang_error.cpp:9:32: error: no member named 'value' in 'Child<int,
> 4>'
> static const int value = DE::value;
>                        ~~~~^
> crtp_clang_error.cpp:27:16: note: in instantiation of template class
> 'Parent<Child<int, 4> >' requested here
> struct Child : Parent< Child<T,N> >
>            ^
> crtp_clang_error.cpp:38:16: note: in instantiation of template class
> 'Child<int, 4>' requested here
>   Child<int,4> c;
>            ^
> crtp_clang_error.cpp:40:3: error: no member named 'print_value' in
> 'Child<int, 4>'; did you mean 'Parent<Child<int, 4> >::print_value'?
>   Child<int,4>::print_value();
>   ^~~~~~~~~~~~~~~~~~~~~~~~~
>   Parent<Child<int, 4> >::print_value
> crtp_clang_error.cpp:11:15: note: 'Parent<Child<int, 4> >::print_value'
> declared here
>   static void print_value ()
>
> Would kindly request some insight into this behaviour.
>
> Best,
> Dipto
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160302/3256a6cf/attachment.html>


More information about the cfe-dev mailing list