Re: template class with default parameter in template parameter declaration

2011-11-09 Thread Marc Glisse

On Tue, 8 Nov 2011, Marc Glisse wrote:


On Tue, 8 Nov 2011, Ulrich Drepper wrote:


Complicated title, here's a bit of code:

#ifdef FIX
# define PARM2 , class T5
#else
# define PARMS2
#endif


template
struct cl1 {
};

template class T4 = cl1>
struct cl2 {
};

cl2<> var;

If compiled without FIX defined this will fail with gcc 4.3 and later.
Haven't checked 4.2 but it works without the fix with gcc 4.1.  The
strange thing is that it also worked with ancient compilers before the
C++ frontend rewrite (gcc 3.2).  In short, when a expecting a template
class in a template parameter list it now is not possible anymore to
skip the default parameters.  Since this is an actual use of the class
(in case the default is used) and the programmer declares to have no
interest in the type of the second template parameter I wouldn't say
it is needed but I haven't tracked a statement in the standard.

Before changing too much code I want to make sure this new and very
old behavior is what is required by the standard and not a bug which
slipped in again.


It is announced in:
http://gcc.gnu.org/gcc-4.2/changes.html

It broke quite a bit of code (in particular with std::vector as cl1), but 
there were some exotic pieces of legal code that were broken by this 
extension, so it had to go.


Notice that in C++11 mode, g++ accepts:

#include 
template class V> struct A {
typedef V T;
};
int main(){
A::T t;
}

where it matches vector as V having 2 parameters but lets you use it with 
one. Don't know if you can rely on it though.


--
Marc Glisse


Re: template class with default parameter in template parameter declaration

2011-11-08 Thread Andrew Pinski
On Tue, Nov 8, 2011 at 10:53 AM, Marc Glisse  wrote:
> On Tue, 8 Nov 2011, Ulrich Drepper wrote:
>
>> Complicated title, here's a bit of code:
>>
>> #ifdef FIX
>> # define PARM2 , class T5
>> #else
>> # define PARMS2
>> #endif
>>
>>
>> template
>> struct cl1 {
>> };
>>
>> template class T4 = cl1>
>> struct cl2 {
>> };
>>
>> cl2<> var;
>>
>> If compiled without FIX defined this will fail with gcc 4.3 and later.
>> Haven't checked 4.2 but it works without the fix with gcc 4.1.  The
>> strange thing is that it also worked with ancient compilers before the
>> C++ frontend rewrite (gcc 3.2).  In short, when a expecting a template
>> class in a template parameter list it now is not possible anymore to
>> skip the default parameters.  Since this is an actual use of the class
>> (in case the default is used) and the programmer declares to have no
>> interest in the type of the second template parameter I wouldn't say
>> it is needed but I haven't tracked a statement in the standard.
>>
>> Before changing too much code I want to make sure this new and very
>> old behavior is what is required by the standard and not a bug which
>> slipped in again.
>
> It is announced in:
> http://gcc.gnu.org/gcc-4.2/changes.html
>
> It broke quite a bit of code (in particular with std::vector as cl1), but
> there were some exotic pieces of legal code that were broken by this
> extension, so it had to go.

To expand on that
http://gcc.gnu.org/gcc-4.1/changes.html
Says it was an undocumented extension which was depercated in 4.1 and
will be removed in a later version.
Thanks,
Andrew Pinski


Re: template class with default parameter in template parameter declaration

2011-11-08 Thread Marc Glisse

On Tue, 8 Nov 2011, Ulrich Drepper wrote:


Complicated title, here's a bit of code:

#ifdef FIX
# define PARM2 , class T5
#else
# define PARMS2
#endif


template
struct cl1 {
};

template class T4 = cl1>
struct cl2 {
};

cl2<> var;

If compiled without FIX defined this will fail with gcc 4.3 and later.
Haven't checked 4.2 but it works without the fix with gcc 4.1.  The
strange thing is that it also worked with ancient compilers before the
C++ frontend rewrite (gcc 3.2).  In short, when a expecting a template
class in a template parameter list it now is not possible anymore to
skip the default parameters.  Since this is an actual use of the class
(in case the default is used) and the programmer declares to have no
interest in the type of the second template parameter I wouldn't say
it is needed but I haven't tracked a statement in the standard.

Before changing too much code I want to make sure this new and very
old behavior is what is required by the standard and not a bug which
slipped in again.


It is announced in:
http://gcc.gnu.org/gcc-4.2/changes.html

It broke quite a bit of code (in particular with std::vector as cl1), but 
there were some exotic pieces of legal code that were broken by this 
extension, so it had to go.


--
Marc Glisse


template class with default parameter in template parameter declaration

2011-11-08 Thread Ulrich Drepper
Complicated title, here's a bit of code:

#ifdef FIX
# define PARM2 , class T5
#else
# define PARMS2
#endif


template
struct cl1 {
};

template class T4 = cl1>
struct cl2 {
};

cl2<> var;

If compiled without FIX defined this will fail with gcc 4.3 and later.
 Haven't checked 4.2 but it works without the fix with gcc 4.1.  The
strange thing is that it also worked with ancient compilers before the
C++ frontend rewrite (gcc 3.2).  In short, when a expecting a template
class in a template parameter list it now is not possible anymore to
skip the default parameters.  Since this is an actual use of the class
(in case the default is used) and the programmer declares to have no
interest in the type of the second template parameter I wouldn't say
it is needed but I haven't tracked a statement in the standard.

Before changing too much code I want to make sure this new and very
old behavior is what is required by the standard and not a bug which
slipped in again.


Re: template class

2011-06-10 Thread Ruben Safir
FWIW

 Array( const Array&rhs );



On Fri, Jun 10, 2011 at 08:30:05PM -0700, eric wrote:
> Dear Mr. Bjarne Stroustrup:
> 
>   Thanks your suggestion, I follow it but it show fatal error:Array.cpp:
> no such file
> so
> I add #include "Array.cpp" in my main program, pg52.cpp
> then
> it can compile
> but
> when I run it, it response
> Segmentation fault
> 
> again it's g++ 4.5.2.  What may cause wrong?
> 
> hope to see your suggestion/advice again, and thanks a lot in advance,
> Eric
> -
> 
> On Fri, 2011-06-10 at 22:18 -0400, bs wrote:
> > On 6/10/2011 9:43 PM, eric wrote:
> > > Dear Bjarne Stroustrup:
> > >
> > >I copied a piece of codes from book(C++ primer 3rd Ed) chapter 2 about
> > > page 52.
> > > but it failed to be compiled under my g++ 4.5.2
> > > here is the program
> > 
> > That book is an antique, but I think that the problem may simply be that 
> > you can't separately compile pg52.cpp and Array.cpp. Try 
> > #include in Array.h .
> > 
> > > ---
> > >
> > > eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
> > > /tmp/ccE9MPMg.o: In function `main':
> > > pg52.cpp:(.text+0x23): undefined reference to `Array::Array(int)'
> > > pg52.cpp:(.text+0x37): undefined reference to
> > > `Array::Array(int)'
> > > pg52.cpp:(.text+0x4b): undefined reference to `Array::Array(int)'
> > > collect2: ld returned 1 exit status
> > > --
> > > #include
> > > #include "Array.h"
> > >
> > >
> > >
> > > int main() {
> > >const int array_size = 4;
> > >
> > >// elemType becomes int
> > >Array  ia(array_size);
> > >
> > >// elemType becomes double
> > >Array  da(array_size);
> > >
> > >// elemType becomes char
> > >Array  ca(array_size);
> > >
> > > int ix;
> > >
> > > for ( ix = 0; ix<  array_size; ++ix ) {
> > >   ia[ix] = ix;
> > >   da[ix] = ix * 1.75;
> > >   ca[ix] = ix + 'a';
> > >  }
> > >
> > >  for ( ix = 0; ix<  array_size; ++ix )
> > >std::cout<<  "[ "<<  ix<<  " ]   ia: "<<  ia[ix]
> > > <<  "\tca: "<<  ca[ix]
> > >     <<  "\tda: "<<  da[ix]<<  std::endl;
> > >
> > >   return 0;
> > > }
> > > ---
> > > // Array.cpp
> > >
> > > #include "Array.h"
> > >
> > > /*
> > > Array::Array(elemType *iarray, int iarray_size)
> > > {
> > >ia = iarray;
> > >_size = iarray_size;
> > > }
> > > */
> > >
> > > template  Array::Array(const Array  &rhs )
> > > {
> > >ia = rhs;
> > > }
> > >
> > > template  Array::Array(const int size )
> > > {
> > >_size = size;
> > > }
> > >
> > >
> > > --
> > > #ifndef Array_H
> > > #define Array_H
> > >
> > >
> > > template<  class elemType>
> > > class Array {
> > > public:
> > >// parameterize element type
> > >explicit Array( int size = DefaultArraySize );
> > >Array( elemType *array, int array_size );
> > >Array( const Array&rhs );
> > >
> > >virtual ~Array() { delete [] ia; }
> > >
> > >bool operator==( const Array&  ) const;
> > >bool operator!=( const Array&  ) const;
> > >
> > >Array&  operator=( const Array&  );
> > >int size() const { return _size; }
> > >
> > >/* virtual */ elemType&  operator[](int index){ return ia[index]; }
> > >/*
> > >virtual void sort();
> > >
> > >virtual elemType min() const;
> > >virtual elemType max() const; */
> > >/* virtual */ int  find( const elemType&value ) const;
> > >
> > > protected:
> > > static const int DefaultArraySize = 12;
> > >
> > > int _size;
> > > elemType *ia;
> > > };
> > >
> > &g

Re: template class

2011-06-10 Thread Ian Lance Taylor
eric  writes:

>   Thanks your suggestion, I follow it but it show fatal error:Array.cpp:
> no such file
> so
> I add #include "Array.cpp" in my main program, pg52.cpp
> then
> it can compile
> but
> when I run it, it response
> Segmentation fault
>
> again it's g++ 4.5.2.  What may cause wrong?
>
> hope to see your suggestion/advice again, and thanks a lot in advance,

This message is not appropriate for the mailing list gcc@gcc.gnu.org.
Please do not send C++ questions to this mailing list.  Thanks.

Ian


Re: template class

2011-06-10 Thread eric
Dear Mr. Bjarne Stroustrup:

  Thanks your suggestion, I follow it but it show fatal error:Array.cpp:
no such file
so
I add #include "Array.cpp" in my main program, pg52.cpp
then
it can compile
but
when I run it, it response
Segmentation fault

again it's g++ 4.5.2.  What may cause wrong?

hope to see your suggestion/advice again, and thanks a lot in advance,
Eric
-

On Fri, 2011-06-10 at 22:18 -0400, bs wrote:
> On 6/10/2011 9:43 PM, eric wrote:
> > Dear Bjarne Stroustrup:
> >
> >I copied a piece of codes from book(C++ primer 3rd Ed) chapter 2 about
> > page 52.
> > but it failed to be compiled under my g++ 4.5.2
> > here is the program
> 
> That book is an antique, but I think that the problem may simply be that 
> you can't separately compile pg52.cpp and Array.cpp. Try 
> #include in Array.h .
> 
> > ---
> >
> > eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
> > /tmp/ccE9MPMg.o: In function `main':
> > pg52.cpp:(.text+0x23): undefined reference to `Array::Array(int)'
> > pg52.cpp:(.text+0x37): undefined reference to
> > `Array::Array(int)'
> > pg52.cpp:(.text+0x4b): undefined reference to `Array::Array(int)'
> > collect2: ld returned 1 exit status
> > --
> > #include
> > #include "Array.h"
> >
> >
> >
> > int main() {
> >const int array_size = 4;
> >
> >// elemType becomes int
> >Array  ia(array_size);
> >
> >// elemType becomes double
> >Array  da(array_size);
> >
> >// elemType becomes char
> >Array  ca(array_size);
> >
> > int ix;
> >
> > for ( ix = 0; ix<  array_size; ++ix ) {
> >   ia[ix] = ix;
> >   da[ix] = ix * 1.75;
> >   ca[ix] = ix + 'a';
> >  }
> >
> >  for ( ix = 0; ix<  array_size; ++ix )
> >std::cout<<  "[ "<<  ix<<  " ]   ia: "<<  ia[ix]
> > <<  "\tca: "<<  ca[ix]
> > <<  "\tda: "<<  da[ix]<<  std::endl;
> >
> >   return 0;
> > }
> > -------
> > // Array.cpp
> >
> > #include "Array.h"
> >
> > /*
> > Array::Array(elemType *iarray, int iarray_size)
> > {
> >ia = iarray;
> >_size = iarray_size;
> > }
> > */
> >
> > template  Array::Array(const Array  &rhs )
> > {
> >ia = rhs;
> > }
> >
> > template  Array::Array(const int size )
> > {
> >_size = size;
> > }
> >
> >
> > --
> > #ifndef Array_H
> > #define Array_H
> >
> >
> > template<  class elemType>
> > class Array {
> > public:
> >// parameterize element type
> >explicit Array( int size = DefaultArraySize );
> >Array( elemType *array, int array_size );
> >Array( const Array&rhs );
> >
> >virtual ~Array() { delete [] ia; }
> >
> >bool operator==( const Array&  ) const;
> >bool operator!=( const Array&  ) const;
> >
> >Array&  operator=( const Array&  );
> >int size() const { return _size; }
> >
> >/* virtual */ elemType&  operator[](int index){ return ia[index]; }
> >/*
> >virtual void sort();
> >
> >virtual elemType min() const;
> >virtual elemType max() const; */
> >/* virtual */ int  find( const elemType&value ) const;
> >
> > protected:
> > static const int DefaultArraySize = 12;
> >
> > int _size;
> > elemType *ia;
> > };
> >
> > #endif
> > ---
> > I asked mailing list in gcc, but so far I did not get any useful
> > response.  I know the book's author Stanley B. Lippman or Josee Lajoie
> > ever work with you.  And I tried to reach Stanley B. Lippman, I can not
> > find his email addreess.  I emailed Josee Lajoie but again never get any
> > message from her (for 2 or 3 days).
> > hope to get your help on this problem.
> > Thanks your efforts a lot in advance
> > Eric
> >
> >
> 




Re: A forward declaration used by template class issue on gcc 4.4.1

2009-10-23 Thread Ian Lance Taylor
"Zhang Lin"  writes:

> class ACE_Message_Queue_NT;

> template 
> ACE_Message_Queue_NT *ACE_Message_Queue_Factory::create_NT_message_queue 
> (int max_threads)
> {
>  ACE_Message_Queue_NT *tmp = 0;
>
>  tmp = new ACE_Message_Queue_NT (max_threads);
>
>  return tmp;
> }
>
> class ACE_Message_Queue_NT
> {
> public:
>  ACE_Message_Queue_NT (int)
>  {
>  }
> };

> This code can be accepted by Microsoft VC 7.1, but GCC reported the following 
> error:
> main.cpp: In static member function 'static ACE_Message_Queue_NT* 
> ACE_Message_Queue_Factory::create_NT_message_queue(int)':
> main.cpp:15: error: invalid use of incomplete type 'struct 
> ACE_Message_Queue_NT'
> main.cpp:1: error: forward declaration of 'struct ACE_Message_Queue_NT'
>
> Is there any description about the error above in C++ standard?

This question is not appropriate for the mailing list g...@gcc.gnu.org.
It would be appropriate for gcc-h...@gcc.gnu.org.  Please take any
followups to gcc-help.  Thanks.


C++ templates are not macros.  Non-dependent names are looked up at
the point of the template definition.  They are not looked up at the
point of template use.  The C++ standard specifies that so that
templates behave consistently rather than changing behaviour depending
upon when they are instantiated.  For more information, look for
descriptions of two-phase lookup.  I don't use the Microsoft compiler,
but I've heard reports that it does not implement two-phase lookup
correctly.

Ian


A forward declaration used by template class issue on gcc 4.4.1

2009-10-23 Thread Zhang Lin
Hello,
I have encountered an issue when building ACE with MinGW and GCC 4.4.1.
The following example can reproduce the issue:
==
main.cpp
==
class ACE_Message_Queue_NT;

template
class ACE_Message_Queue_Factory
{
public:
 static ACE_Message_Queue_NT *create_NT_message_queue (int max_threads);
};

template 
ACE_Message_Queue_NT *ACE_Message_Queue_Factory::create_NT_message_queue 
(int max_threads)
{
 ACE_Message_Queue_NT *tmp = 0;

 tmp = new ACE_Message_Queue_NT (max_threads);

 return tmp;
}

class ACE_Message_Queue_NT
{
public:
 ACE_Message_Queue_NT (int)
 {
 }
};

int main(void)
{
 ACE_Message_Queue_Factory::create_NT_message_queue(100);

 return 0;
}

This code can be accepted by Microsoft VC 7.1, but GCC reported the following 
error:
main.cpp: In static member function 'static ACE_Message_Queue_NT* 
ACE_Message_Queue_Factory::create_NT_message_queue(int)':
main.cpp:15: error: invalid use of incomplete type 'struct ACE_Message_Queue_NT'
main.cpp:1: error: forward declaration of 'struct ACE_Message_Queue_NT'

Is there any description about the error above in C++ standard?

Thanks.

Best Regards,
Lin Zhang
2009-10-24


Re: Possible 4.4.0 regression (can't match template ctor of template class where...)

2009-03-04 Thread Sebastian Redl
Simon Hill wrote:
> g++ doesn't seem able to match a template ctor of a template class
> where the ctor input is an internal class defined inside any template
> class.
>
> I briefly skimmed the titles of the current regression issues and I
> didn't notice any that matched this, but I'm not 100% sure.
>
> I first posted this here thinking it was my code that was somehow wrong.
> http://www.gamedev.net/community/forums/topic.asp?topic_id=526736
>
> demo.cpp
> 
> template 
> class CFoo
>   {
> public:
>   class CZep {};
>
>   CFoo(int); // line 7.
>   template  CFoo(typename CFoo::CZep); // ## ctor I want ##
>   ~CFoo();
>
>   CZep zep();
>   };
>   
Imagine the compiler has to deal with this specialization of CFoo:

template <>
class CFoo {
  public:
typedef CFoo::CZep CZep;

// otherwise identical
};

then the call

CFoo::CZep cz;
CFoo cf(cz);

is ambiguous - both of these are valid:
CFoo::CFoo(CFoo::CZep)
CFoo::CFoo(CFoo::CZep)



The language cuts this short, though. Containing types of types in the
argument do not take part in template argument deduction. Z cannot be
deduced from the arguments; it must be specified explicitly. Since
that's not possible for a constructor (or at least I don't know how),
the constructor is completely unusable.

GCC 4 is right here.

Sebastian


Re: Possible 4.4.0 regression (can't match template ctor of template class where...)

2009-03-04 Thread Richard Guenther
On Wed, Mar 4, 2009 at 9:12 PM, Simon Hill  wrote:
> g++ doesn't seem able to match a template ctor of a template class
> where the ctor input is an internal class defined inside any template
> class.
>
> I briefly skimmed the titles of the current regression issues and I
> didn't notice any that matched this, but I'm not 100% sure.
>
> I first posted this here thinking it was my code that was somehow wrong.
> http://www.gamedev.net/community/forums/topic.asp?topic_id=526736
>
> demo.cpp
> 
> template 
> class CFoo
>  {
>    public:
>      class CZep {};
>
>      CFoo(int); // line 7.
>      template  CFoo(typename CFoo::CZep); // ## ctor I want ##
>      ~CFoo();
>
>      CZep zep();
>  };
>
>
> int main()
>  {
>    CFoo x(1);
>    CFoo::CZep z;
>    CFoo w(z); // line 19: ## should call the ctor on line 8 ##
>
>    return 0;
>  }
>
> 
>
> g++ demo.cpp
> 
> demo.cpp: In function ‘int main()’:
> demo.cpp:19: error: no matching function for call to
> ‘CFoo::CFoo(CFoo::CZep&)’
> demo.cpp:7: note: candidates are: CFoo::CFoo(int) [with T = int]
> demo.cpp:3: note:                 CFoo::CFoo(const CFoo&)
> 
>
> Specs:
> g++ [4.4.0]
> ubuntu [ibex, 64]
>
>
> This compiles fine on 3.3.2 and MSVC according to other testers at 
> gamedev.net.
>
>
> Note: In the above example CZep is inside CFoo, but it doesn't matter
> if you put CZep inside class CBar {} instead, just as long as it's
> inside another template class.
>
>
>
> Does the above code compile OK on other people's 4.4.0s? or is it a bug?

EDG says

t.C(8): warning #488: template parameter "Z" is not used in declaring
the parameter types of function template
"CFoo::CFoo(CFoo::CZep)"
  template  CFoo(typename CFoo::CZep); // ## ctor I want ##
 ^

t.C(8): warning #488: template parameter "Z" is not used in declaring
the parameter types of function template
"CFoo::CFoo(CFoo::CZep) [with T=int]"
  template  CFoo(typename CFoo::CZep); // ## ctor I want ##
 ^
  detected during instantiation of class "CFoo [with
T=int]" at line 17

t.C(19): error: no instance of constructor "CFoo::CFoo [with
T=int]" matches the argument list
argument types are: (CFoo::CZep)
CFoo w(z); // line 19: ## should call the ctor on line 8 ##
^

compilation aborted for t.C (code 2)

MSVC wasn't ever a good source of C++ validity testing.  Basically during
instantiation of CFoo the templated constructor is not instantiated and
thus not found.

Richard.


Possible 4.4.0 regression (can't match template ctor of template class where...)

2009-03-04 Thread Simon Hill
g++ doesn't seem able to match a template ctor of a template class
where the ctor input is an internal class defined inside any template
class.

I briefly skimmed the titles of the current regression issues and I
didn't notice any that matched this, but I'm not 100% sure.

I first posted this here thinking it was my code that was somehow wrong.
http://www.gamedev.net/community/forums/topic.asp?topic_id=526736

demo.cpp
====
template 
class CFoo
  {
public:
  class CZep {};

  CFoo(int); // line 7.
  template  CFoo(typename CFoo::CZep); // ## ctor I want ##
  ~CFoo();

  CZep zep();
  };


int main()
  {
CFoo x(1);
CFoo::CZep z;
CFoo w(z); // line 19: ## should call the ctor on line 8 ##

return 0;
  }



g++ demo.cpp

demo.cpp: In function ‘int main()’:
demo.cpp:19: error: no matching function for call to
‘CFoo::CFoo(CFoo::CZep&)’
demo.cpp:7: note: candidates are: CFoo::CFoo(int) [with T = int]
demo.cpp:3: note: CFoo::CFoo(const CFoo&)


Specs:
g++ [4.4.0]
ubuntu [ibex, 64]


This compiles fine on 3.3.2 and MSVC according to other testers at gamedev.net.


Note: In the above example CZep is inside CFoo, but it doesn't matter
if you put CZep inside class CBar {} instead, just as long as it's
inside another template class.



Does the above code compile OK on other people's 4.4.0s? or is it a bug?


Re: c++0x {g++}4.4, =default definition outside template class fails{ to link}.

2008-08-26 Thread Paolo Carlini

Simon Hill wrote:

Sorry Paolo, that definitely should have been.
  

no problem.

Is this a bug?
I think so. I opened c++/37234, you may want to add yourself to the CC 
list of the PR.


Paolo.


Re: c++0x {g++}4.4, =default definition outside template class fails{ to link}.

2008-08-25 Thread Simon Hill
Sorry Paolo, that definitely should have been.

=== THIS CODE FAILS TO LINK ===
template 
class foo {
  public:
foo() =default; // <<--- this works fine.
~foo();
};

template 
foo::~foo() =default; <<--- ERROR: This doesn't get built by the compiler.
// foo::~foo() {}; <<-- replacing with this line fixes the error.

int main() {

foo fi;

return 0;
}
===

Not
=== THIS CODE FAILS TO COMPILE ===
...


Still, shouldn't the  version of
"foo::~foo() =default;"
be built by the compiler since it's called by main().

Is this a bug? Should I update my gcc and go retest?
Does it fail to link for you? (or anyone else)?


Also, yes I am using a snapshot, I couldn't figure out how to get the
mainline (trunk?) 4.4 version, all I got was 4.3 using svn. Then again
I'm new to svn and building gcc.


Re: c++0x 4.4, =default definition outside template class fails.

2008-08-24 Thread Paolo Carlini

Hi,

=== THIS CODE FAILS TO COMPILE ===
template 
class foo {
  public:
foo() =default; // <<--- this works fine.
~foo();
};

template 
foo::~foo() =default; <<--- ERROR: This doesn't get built by the compiler.
// foo::~foo() {}; <<-- replacing with this line fixes the error.

int main() {

foo fi;

return 0;
}
===
  
The above compiles just fine for me. Are you using current mainline or 
by chance an old snapshot?


Paolo.


Re: c++0x 4.4, =default definition outside template class fails.

2008-08-24 Thread Paolo Carlini
.. and I would suggest filing a normal Bugzilla PR, to the attention of 
[EMAIL PROTECTED] in CC, because it seems indeed a bug to me: we even 
have in the testsuite a very similar run-type test different only 
because the class isn't a template.


Paolo.


Re: c++0x 4.4, =default definition outside template class fails.

2008-08-24 Thread Paolo Carlini

.. never mind, the problem you are seeing is at *link* time, ok...

Paolo.


c++0x 4.4, =default definition outside template class fails.

2008-08-23 Thread Simon Hill
I've just installed g++ 4.4 and I'm playing with some of the c++0x features.

The below page describes the =default and =delete syntax. (Used to
manipulate the special member functions default/copy constructor,
destructor and copy assign).
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm

I've managed to get parts of my code working, but it seems that
defining as default a template class's special member functions
outside the class definition doesn't work.
I'm not really sure if it should, but looking at the suggested
modifications to the spec [8.4 paragraph 7] (in the above document), I
_think_ it's saying that it should work.

The problem is that the compiler fails to generate the function
(foo::~foo() in the following code), which causes the linker to
report an undefined reference.

I didn't post this as a bug report as I'm not 100% sure it is a bug.


=== THIS CODE FAILS TO COMPILE ===
template 
class foo {
  public:
foo() =default; // <<--- this works fine.
~foo();
};

template 
foo::~foo() =default; <<--- ERROR: This doesn't get built by the compiler.
// foo::~foo() {}; <<-- replacing with this line fixes the error.

int main() {

foo fi;

return 0;
}
===


=== THIS CODE COMPILES ===
class foo {
  public:
foo() =default;
~foo();
};

foo::~foo() =default;

int main() {

foo fi;
return 0;

}
===

NOTE: The reason I noticed this was that I was trying to default a
virtual destructor.
This should work, but I'm guessing that it must be done outside the
function definition, because it doesn't work inside.
However, if the above is a bug, this may also be a bug.


Why g++ does not emit any information for a local variable in a template class member function?

2008-06-03 Thread Peng Yu
Hi,

I have the following code. I try to print the variable temp in the
constructor of A. But gdb can not do that. The error message is shown
blow the C++ code.

According to the people from gdb mailing list, it is because that the
compiler (g++)  does not generate information for such variable in the
debugging mode. Is it a bug of g++. BTW, I'm using g++ of the
following version

$ g++ --version
g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Thanks,
Peng

template 
class A {
 public:
   A(int a) {
 T temp = a + 1;
 _a = temp + 1;
 }
 private:
   T _a;
};

int main() {
 A a(1);
}


$ gdb main
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) start
Breakpoint 1 at 0x400510: file main.cc, line 13.
main () at main.cc:13
13A a(1);
(gdb) s
A (this=0x7fff250d7080, a=1) at main.cc:5
5 T temp = a + 1;
(gdb) n
6 _a = temp + 1;
(gdb) p temp
No symbol "temp" in current context.
(gdb)


Re: template class scoping rules

2006-03-14 Thread David Fang
Hi,

Since 3.4, (template-)dependent lookup has been changed to conform
to the standard.  In particular, from http://gcc.gnu.org/gcc-3.4/changes.html:

"In a template definition, unqualified names will no longer find members
of a dependent base."

This allows lookups to be bound at template instantiation time.
(Consider the case where Two is specialized after Three is
defined, but before Three is instantiated.)

Two is a dependent base of Three.  Another way of qualifying the
name to lookup is to use "this->m_Public".

HTH

Fang


> test.cpp: In constructor 'Three::Three()':
> test.cpp:20: error: 'm_Public' was not declared in this scope
>
> class One
> {
> public:
>   One();
>       ~One();
>
> public:
>   int m_Public;
> };
>
> template  class Two : public One
> {
> public:
>   Two() {m_Public = 0;}
> };
>
> template  class Three : public Two
> {
> public:
>   Three() {m_Public = 0;}
> };




template class scoping rules

2006-03-14 Thread Matthew J Fletcher
Hi

This cut down example does not compile with gcc 3.4.x / 4.0.x or 4.1.0.

test.cpp: In constructor 'Three::Three()':
test.cpp:20: error: 'm_Public' was not declared in this scope

It does compile with VS2005 / VS6

class One
{
public:
One();
~One();

public:
int m_Public;
};

template  class Two : public One
{
public:
Two() {m_Public = 0;}
};

template  class Three : public Two
{
public:
Three() {m_Public = 0;}
};

this fixes it.

template  class Three : public Two
{
public:
Three() {Two::m_Public = 0;}
};

any ideas ?

regards
---
Matthew J Fletcher
Embedded Software
Serck Controls Ltd
---
**
Serck Controls Ltd, Rowley Drive, Coventry, CV3 4FH, UK
Tel: +44 (0) 24 7630 5050   Fax: +44 (0) 24 7630 2437
Web: www.serck-controls.com  Admin: [EMAIL PROTECTED]
A subsidiary of Serck Controls Pty. Ltd. Reg. in England No. 4353634
**
This email and files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they 
are addressed. If you have received this email in error please notify 
the above. Any views or opinions presented are those of the author 
and do not necessarily represent those of Serck Controls Ltd. 


This message has been checked by MessageLabs
**


Re: specific instantiation of static members from template class

2005-11-17 Thread Jonathan Wakely
cedric wrote:

> 
> hello, I have a problem when I try to instantiate static members. this code 
> works with gcc-3.4.5 but not with gcc-4.0.2 (debian sid).
> here a test case with 3 files :
> 
> 
> / main.cpp
> #include 
> #include "Test.h"
> 
> int main(int argc, char **argv)
> {
>   std::cout << TestInt::member << std::endl;
>   return 0;
> }
> 
> / test.h
> template 
> class Test
> {
> public:
> 
>   static T member;
> };

add this line here:
template  T Test::member;

> typedef Test TestInt;
> 
> / test.cpp
> #include "Test.h"
> 
> template class Test;

Either remove this line or place it before the explicit instantiation:

> template<> int Test::member;

I think GCC is right here.

jon


-- 
"God is as real as I am," the old man said.  My faith was restored, for
I knew that Santa would never lie.


specific instantiation of static members from template class

2005-11-17 Thread cedric

hello, I have a problem when I try to instantiate static members. this code 
works with gcc-3.4.5 but not with gcc-4.0.2 (debian sid).
here a test case with 3 files :


/ main.cpp
#include 
#include "Test.h"

int main(int argc, char **argv)
{
std::cout << TestInt::member << std::endl;
return 0;
}

/ test.h
template 
class Test
{
public:

static T member;
};

typedef Test TestInt;

/ test.cpp
#include "Test.h"

template class Test;
template<> int Test::member;


I got an error at link because it cannot find the symbol Test::member 
used in "main.o"
If I use only one file, it works.
If I give an initial value to Test::member in test.cpp, it works.
It works with gcc-3.4.5.

I did some research and see that it could works even without the member 
declaration, the class instantiation should be enough here : 
http://gcc.gnu.org/ml/gcc/2004-05/msg01393.html

Is this a bug or a behavour change from gcc ?