Operating System - OpenVMS
1753878 Members
7487 Online
108809 Solutions
New Discussion юеВ

CXX: Reference to instantiated template bug

 
AlanAntonuk
New Member

CXX: Reference to instantiated template bug

Here's a quick repro on a bug in CXX:

COLOMA-ALAN>cxx/ver
HP C++ V7.3-009 for OpenVMS Alpha V8.3

COLOMA-ALAN>type a.h
#ifndef A_H
#define A_H

template
class Templ
{
public:
T* Func();
};

#endif

COLOMA-ALAN>type a.cpp
#include "a.h"

void someFunc()
{
int* ptr = Templ().Func();
}

COLOMA-ALAN>cxx a.cpp

void someFunc()
.....^
%CXX-E-REDEF, declaration has already been defined by function
"someFunc" (declared at line 3 of
"$1$DGA1269:[AEGANTONUK.temp]a.cpp;1")
at line number 3 in file $1$DGA1269:[AEGANTONUK.temp]a.cpp;1

%CXX-I-MESSAGE, 1 error detected in the compilation of "$1$DGA1269:[AEGANTONUK.temp]a.cpp;1".

====================
Interesting aspects of this bug:
COLOMA-ALAN>RENAME a.cpp b.cpp
COLOMA-ALAN>cxx b.cpp
---- No errors
Another interesting aspect of this bug:
COLOMA-ALAN>COPY a.cpp b.cpp
COLOMA-ALAN>cxx b.cpp

void someFunc()
.....^
%CXX-E-REDEF, declaration has already been defined by function
"someFunc" (declared at line 3 of
"$1$DGA1269:[AEGANTONUK.temp]b.cpp;1")
at line number 3 in file $1$DGA1269:[AEGANTONUK.temp]a.cpp;1

%CXX-I-MESSAGE, 1 error detected in the compilation of "$1$DGA1269:[AEGANTONUK.temp]b.cpp;1".

***** Why is the compiler trying to compile a.cpp?!?!?
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: CXX: Reference to instantiated template bug

I know nothing about C++, but you might try
either not using both "a.h" and "a.cpp" as
file names, or using /NOIMPLICIT_INCLUDE.

HELP CXX /IMPLICIT_INCLUDE
AlanAntonuk
New Member

Re: CXX: Reference to instantiated template bug

/IMPLICIT_INCLUDE seems to be the issue.

Thanks!

That said: having this as a default without any kind diagnostic as to whats going on, while it may make for some backwards compatibility with something; is rather confusing for those who don't know about this compiler switch.


Steven Schweda
Honored Contributor

Re: CXX: Reference to instantiated template bug

> [...] having this as a default [...]

Knowing nothing, I assume that any serious
C++ user would prefer it this way. A visit
to the C++ language reference manual and/or
user guide might be informative.

Learning more (that is, anything useful)
about C++ has been on my to-do list for a
long time. Sadly, laziness is among my many
character flaws.
AlanAntonuk
New Member

Re: CXX: Reference to instantiated template bug

I am proficient at C++, but by no means someone who has explored some of the dark musty corners of the language.

That said HP CXX is the only modern compiler that I've come across that has done this by default (I've used gcc, icc, and visual-c++).

These all require you to explicitly #include anything that the compiler sees.

Anyhow mystery solved.
MarkOfAus
Valued Contributor

Re: CXX: Reference to instantiated template bug

FYI,
on v6.3-020 version of cxx, compiling a.cpp as above gives no errors, but when a.cpp is copied to b.cpp, your error appears:
void someFunc()
.....^
%CXX-E-REDEF, declaration has already been defined by function
"someFunc" (declared at line 3 of

/list produces:
_VMS_VER=70320022 __VMS_VERSION="V7.3-2 " __vms_version="V7.3-2 " __G_FLOAT
__D_FLOAT=0 __vms __VMS __INITIAL_POINTER_SIZE=0 __BIASED_FLT_ROUNDS=2
__32BITS __IEEE_FLOAT=0 __Alpha_AXP _LONGLONG __alpha__ __alpha __ALPHA
__DECCXX_VER=60390020 __MODEL_ARM __STD_ANSI __IMPLICIT_INCLUDE_ENABLED
__STDNEW __X_FLOAT __PRAGMA_ENVIRONMENT __DECCXX __EDG_VERSION__=243 __EDG__
__IMPLICIT_USING_STD __RTTI __EXCEPTIONS __GLOBAL_ARRAY_NEW
__BOOL_IS_A_RESERVED_WORD _BOOL_EXISTS __WCHAR_T _WCHAR_T __cplusplus
__TIME__="13:25:45" __DATE__="Feb 5 2010"

Amusing!
Dennis Handly
Acclaimed Contributor

Re: CXX: Reference to instantiated template bug

>by no means someone who has explored some of the dark musty corners of the language.

Yes, this is from the very musty past.

>These all require you to explicitly #include anything that the compiler sees.

Right. On HP-UX we had the transition from cfront to aC++ but required an option.

>Steven: using /NOIMPLICIT_INCLUDE.

Yes I immediately thought of cfront.

>I assume that any serious C++ user would prefer it this way.

No, any serious C++ programmer knows the C++ Standard says that this cfront coding template coding style is obsolete and probably illegal.
If you want the template bodies, you explicitly include them.
WW304289
Frequent Advisor

Re: CXX: Reference to instantiated template bug

> That said HP CXX is the only modern compiler that I've come across that has done this by default (I've used gcc, icc, and visual-c++).

From what I can see in some configuration file, IBM AIX VisualAge C++ starting with version 5.0.2.0 also does implicit inclusion by default, but I cannot verify it.

I'm not sure about the Alpha compiler, but IPF compiler does not do implicit inclusion in gnu compatibility mode, so, if you are porting stuff from gcc, it may be a good idea to compile it with /stand=gnu.

-Boris

a.h
---
#ifndef A_H
#define A_H

template
class Templ
{
public:
T* Func();
};

#endif

a.cc
----
#include "a.h"

template T* Templ::Func() { return 0; }

a.cpp
-----
#include "a.h"

int main() {
return *Templ().Func();
}

$ cxx a.cpp
$ cxxlink a.obj
$ cxx a.cpp/stand=gnu
$ cxxlink a.obj
%ILINK-W-NUDFSYMS, 1 undefined symbol:
%ILINK-I-UDFSYM, int *Templ ::Func()
%ILINK-W-USEUNDEF, undefined symbol int *Templ ::Func() referenced
source code name: "Templ::Func()"