Possible bug in explicit template specialization

Gabriel Dos Reis gdr@codesourcery.com
Tue Jun 6 05:36:00 GMT 2000


Roman Levenstein <romix@geocities.com> writes:

| Hi,
| 
| I think, I've found a bug in g++.

No, it is a problem in your program.

| Here is the source code(it's a part of an library):
| 
| template <class E>
| class A
| {
| public:
|    A(){}
| 
|    A& assign(E const & obj); // do I need to add "export" here?

Maybe. But g++ doesn't understand `export', right now; and that isn't
the source of the problem.

| 
|    A& operator=(E const & obj)
|    {
|       assign(obj);  
|    }
| };
| 
| void dummy_func()
| {
|    A< int > obj; 
|    obj=4;
        ^
First instantiation here.

|    obj.assign(5); // first instantiation?

No, second instantiation.

| }
| 
| // #1 <- error message 
| A<int>& A<int>::assign(int const & obj)
| {
|   // code of assign
| }

This is invalid.  You're declaring a specialization after an implicit
instantiation. 

| Compiler gives the following error messages:
| 
| templ.cpp:24: specialization of A<int>::assign(const int &) after
| instantiation
| templ.cpp:24: explicit specialization of A<int>::assign(const int &)
| after first use
| 
| I've got the same messages from both gcc-2.95.2 and  gcc-2.96.20000605.
| (And Sun C++ compiler compiles it without problems, according to the
| author of this library)

17.7.3/6
  If a template, a member template or the member of a class template
  is explicitly specialized then that specialization shall be declared
  before the first use of the specialization that would cause an
  implicit instantiation to take place, in every translation unit in
  which such a use occurs; no diagnostic is required.

| The problem is that I can't place the code of assign() before
| dummy_func,
| because they are placed in different source files in the library and the
| code above was produced by preprocessor. It would be rather difficult to
| correct all such declarations in all library files. 

Well, how to organize programs files is beyond the scope of this
list: gcc-bugs@gcc.gnu.org is for dealing with bugs in GCC.
You might want to ask in one of the c++ dedicated Usenet groups
e.g. comp.lang.c++.moderated.

| Even if it's not an error, but a correct behavior of gcc, I'd like to
| know, what is being instantiated at obj.assign(5) as a body of assign()?

A declaration was instantiated from the general declaration.

-- Gaby
CodeSourcery, LLC                             http://www.codesourcery.com


More information about the Gcc-bugs mailing list