Template and inheritance

Template and inheritance

Imagen de qweasd q.

Hello, the following trivial program doesn't compile with icpc, but it's fine with g++ and clang++ :

 template< unsigned char S > class A { public: A(int a) : a(a) { } int a; }; class B : A< 's' > { public: B(int b) : A(b) { } }; 

That explains some of the problems in http://software.intel.com/en-us/forums/topic/401384. Who is wrong, icpc, or g++ and clang++ ?

publicaciones de 20 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.
Imagen de jimdempseyatthecove

How about:

template< unsigned char S >
class A { public: A(int _a) : a(_a) { } int a; };

Jim Dempsey

www.quickthreadprogramming.com
Imagen de qweasd q.

Hello Jim,

I don't see how that would change the outcome of the compilation (and in fact, it doesn't). The problem comes from "error: argument list for class template "A" is missing B(int b) : A(b) { }", but of course the argument of the class template "A" should (IMO) be inferred from the definition of class B. Hence I think icpc is wrong, and clang++ and g++ are right, but maybe this is not the case.

Changing the constructor of B to B(int b) : A<'s'>(b) { }, makes the file compilable, but it shouldn't be necessary to redefine the template argument again, right ?

Imagen de Jennifer J. (Intel)

I do not see this issue with the 14.0 beta update 1. You can still download the beta update1. See this thread about the beta.

Jennifer

Imagen de qweasd q.

Ok thanks, I was indeed using icpc 13.1. Hopefully this will also fix compilation issues with g++ 4.8.1 headers, in the meantime, back to g++.

Imagen de Sergey Kostrov

>>...I do not see this issue with the 14.0 beta update 1...

Did you create an instance of class B? it is reproducible even with older version of Intel C++ compiler::

..\Tests>icl.exe /MD Test33.cpp
Intel(R) C++ Compiler XE for applications running on IA-32, Version 12.1.7.371 Build 20120928
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.

Test33.cpp
Test33.cpp(22): error: no default constructor exists for class "B"
B b;
^

compilation aborted for Test33.cpp (code 2)

What error did you have? Was it with 32-bit or 64-bit compiler?

Test case is attached.

Adjuntos: 

AdjuntoTamaño
Descargar test33.cpp296 bytes
Imagen de Sergey Kostrov

Attached is a modified test cases and take a look. Once again, please post a compilation error you had.

Take into account that different C++ compilers do a different processing when it comes to templates. For example, GCC-like compilers detect errors in templates even if there no an instance declared. Intel, Microsoft, Borland and Turbo C++ compilers do not detect errors in templates if there is no declared instance of some template class.

Outputs are as follows:

[ Compilation ]

Intel(R) C++ Compiler XE for applications running on IA-32, Version 12.1.7.371 Build 20120928
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.

Test33.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

-out:Test33.exe
Test33.obj

[ Runtime ]

..\Tests>Test33.exe
a1.a: 3
b1.a: 5
b2.a: 7

Imagen de Sergey Kostrov

Sorry, I forgot to attach a new version of Test33.cpp file. Here it is.

Adjuntos: 

AdjuntoTamaño
Descargar test33.cpp512 bytes
Imagen de Sergey Kostrov

This is a short follow up.

>>...Who is wrong, icpc, or g++ and clang++?

Please post compilation outputs for all these three compilers using initial test case ( from the 1st post ) and my latest test Test33.cpp ( see previous post ). Thanks.

Imagen de qweasd q.

Hello Sergey,

Your initial test case obviously won't compile with any of the compilers, since there is no default constructor for class B, and you have a line with "B b;", which implies a call to the default constructor.

Your last test case doesn't compile with icpc 13.1, but as Jennifer said, it should be fixed in 14.0. The error is still the same with icpc, "error: argument list for class template "A" is missing"

Imagen de Sergey Kostrov

>>...Your last test case doesn't compile with icpc 13.1, but as Jennifer said, it should be fixed in 14.0. The error is still the same
>>with icpc, "error: argument list for class template "A" is missing"...

Thanks and I will do additional verification with version 13.x for Windows.

Imagen de Sergey Kostrov

Here are results of verification of your initial test-case with a legacy Borland C++ compiler:

...
..\Tests>bcc32.exe Test033b.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Test033b.cpp:
Error E2102 Test033b.cpp 17: Cannot use template 'A' without specifying specialization parameters in function B::B(int)
Error E2272 Test033b.cpp 17: Identifier expected in function B::B(int)
Error E2251 Test033b.cpp 17: Cannot find default constructor to initialize base class 'A<115>' in function B::B(int)
...

*** 3 errors in Compile ***

>>...doesn't compile with icpc, but it's fine with g++ and clang++
>>...
>>...Who is wrong, icpc, or g++ and clang++ ?

So, g++ and clang++ C++ compilers deviated from the C++ standard and by some unknown reason supported Non Standard features ( declarations ). I consider Borland C++ compiler as a Supreme Verificator of fundamental C++ features and compliance / compatibility to 2000-year-like C++ standard(s).

Adjuntos: 

AdjuntoTamaño
Descargar test033b.cpp462 bytes
Imagen de qweasd q.

Source for stating that "g++ and clang++ are deviated from the C++ standard" for that example, please ?

"Cannot use template 'A' without specifying specialization parameters in function B::B(int)" <= well, that's the problem, the specialization parameter for 'A' is specified by the defintion of 'B' !

I attached an example for which I'm curious as to what Borland says about it. The intel compiler error says:

intel.cpp(16): error: "A" is not a nonstatic data member or base class of class "B"
          B(int b) : A< 'd' >(b) { }

meaning that the compiler knows that the specialization parameter is not 'd'.

Adjuntos: 

AdjuntoTamaño
Descargar intel.cpp286 bytes
Imagen de qweasd q.

Source for stating that "g++ and clang++ are deviated from the C++ standard" for that example, please ?

"Cannot use template 'A' without specifying specialization parameters in function B::B(int)" <= well, that's the problem, the specialization parameter for 'A' is specified by the defintion of 'B' !

I attached an example for which I'm curious as to what Borland says about it. The intel compiler error says:

intel.cpp(16): error: "A" is not a nonstatic data member or base class of class "B"
          B(int b) : A< 'd' >(b) { }

meaning that the compiler knows that the specialization parameter is not 'd'.

Adjuntos: 

AdjuntoTamaño
Descargar intel.cpp286 bytes
Imagen de Casey

>> Source for stating that "g++ and clang++ are deviated from the C++ standard" for that example, please ?

GCC does support exntensions to the published standards and those extensions are listed in the GCC documentation.  The extensions are enabled by default but you can control thier inclusion with the -std= flag.  Legal options (in gcc 4.7.3) include c89,c90,c99,gnu89,gnu90,gnu99,c++98,c++0x,gnu++98,gnu++0x.  The default standard for C code is gnu89 and for C++ code gnu++98 so by default the compiler will accept certain extensions to the standard.  

Try compiling your code in g++ with -std=c++98 and see if it still compiles.  That will narrow down if the problem is in standards compliance or with the compilers implementations of the standard.

Imagen de Sergey Kostrov

>>...Try compiling your code in g++ with -std=c++98 and see if it still compiles...

Why wouldn't you do your won verifications with as many as possible C++ compilers in order to understnand what could be possibly wrong and to help all the rest users?

Imagen de qweasd q.

Cita:

Casey wrote:

>> Source for stating that "g++ and clang++ are deviated from the C++ standard" for that example, please ?

GCC does support exntensions to the published standards and those extensions are listed in the GCC documentation.

That is why I was asking for a source on the C++ standard about that specific example, not about the compiler itself, sorry if it was not clear. (btw, it also compiles with c++98)

Imagen de Sergey Kostrov

>>...I attached an example for which I'm curious as to what Borland says about it...

..\Tests>bcc32.exe intel.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
intel.cpp:
Error E2312 intel.cpp 16: 'A<100>' is not an unambiguous base class of 'B' in function B::B(int)
Error E2251 intel.cpp 16: Cannot find default constructor to initialize base class 'A<115>' in function B::B(int)
*** 2 errors in Compile ***

You're Not arguing with me ( have you ever used Borland C++ compiler? ) and I see that GCC and Clang teams are trying to "undermine" some fundamental features of C++ language and, unfortunately, I can't justify such deviations. I regret to see that Intel C++ compiler team follows that wrong path. It would be nice to see additional information on why they decided to support it by default without creating additional compiler flags, like '-full-clang-compatibility' or '-full-gcc-compatibility' ( similar to an option to provide a full compatibility with Microsoft C++ compiler ).

Note: Please download Borland C++ compiler version 5.5.1 for all the rest verifications on your side(s).

Imagen de qweasd q.

Cita:

Sergey Kostrov wrote:

>>...I attached an example for which I'm curious as to what Borland says about it...

..\Tests>bcc32.exe intel.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
intel.cpp:
Error E2312 intel.cpp 16: 'A<100>' is not an unambiguous base class of 'B' in function B::B(int)
Error E2251 intel.cpp 16: Cannot find default constructor to initialize base class 'A<115>' in function B::B(int)
*** 2 errors in Compile ***

You're Not arguing with me ( have you ever used Borland C++ compiler? ) and I see that GCC and Clang teams are trying to "undermine" some fundamental features of C++ language and, unfortunately, I can't justify such deviations. I regret to see that Intel C++ compiler team follows that wrong path. It would be nice to see additional information on why they decided to support it by default without creating additional compiler flags, like '-full-clang-compatibility' or '-full-gcc-compatibility' ( similar to an option to provide a full compatibility with Microsoft C++ compiler ).

Note: Please download Borland C++ compiler version 5.5.1 for all the rest verifications on your side(s).

Sergey, I would like to assure you that I'm not arguing with you in any way. I just want to understand the problem here. What is not clear from my side, is that you are saying that G++ and clang++ (and now icpc) are violating the C++ standards. My previous question was then to naturally ask for a "proof" of that violation, by just giving me a pointer to the standards about that specific example. Do you have such a pointer, or are you just inferring that there is a violation because the Borland compiler can't compile the example ?

Thanks a lot for your help and time.

Imagen de Sergey Kostrov

>>...My previous question was then to naturally ask for a "proof" of that violation, by just giving me a pointer to the standards
>>about that specific example. Do you have such a pointer, or are you just inferring that there is a violation because
>>the Borland compiler can't compile the example?

1. Borland C++ doesn't allow such declarations and needs explicit declarations for all cases like this one, I mean when default constructors are needed, and that is why it fails.

2. If this is a new feature of C++11 Standard, or some another C/C++ Standard, then it needs to be confirmed. Of course, Borland C++ compiler doesn't support it and it will never support.

3. My understanding is that GCC and Clang C++ compilers, and possibly all the rest modern C++ compilers, implicitly create a default constructor and that is why the test case could be successfully compiled.

4. I won't be surprised if Microsoft C++ compilers also support that feature, I mean related to your latest test case, and please do your own verifications.

5. In case of Intel C++ compiler I don't see that feature listed on: http://software.intel.com/en-us/articles/c0x-features-supported-by-intel....

Here are a couple of advises ( please ignore them if you understand everything ):

- Take a look at C++ Standards published on the Internet and released between 1990 and 2001 years, or later between 2001 and 2010 years

- Try to contact developers of GCC and Clang C++ compilers with questions why they allowed to create an instance of the object in that case when a default constructor needs to be declared. If this is a part of some C/C++ Standard then this is what you need and it will explain all these compilation errors

Thanks for your time.

Inicie sesión para dejar un comentario.