public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class
@ 2005-01-06  8:40 jean-pierre dot chevallet at imag dot fr
  2005-01-06 10:13 ` [Bug c++/19288] " giovannibajo at libero dot it
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jean-pierre dot chevallet at imag dot fr @ 2005-01-06  8:40 UTC (permalink / raw)
  To: gcc-bugs

#include <iostream>

using namespace std;

template <typename T> class C1 {
public:
  T V;
  C1(T v) : V(v) {};
  
  class nested {
  public:
    T W;
    nested(T w) : W(w) {};
  };
  
};

// This works well

template <typename T>
T f1 ( C1<T>& c) { return c.V; }

// the problem is HERE

template <typename T>
T f2 ( typename C1<T>::nested& c) { return c.W; }

// This is correct with gcc 3.3.2 20031022
// This is incorrect with gcc 3.4.2 20041017
// This is incorrect with gcc 3.4.3 20041226
// This is incorrect with gcc4 4.0.0 20041228
// Error :
// nestedFriendTemplateFunction.cc:44: error: no matching function for call to
`f2(C1<float>::nested&)'
// Need EXPLICITE instantiation ==> GCC BUG !!!
// float f2 (C1<float>::nested& c) { return c.W; }


int main() {
  C1<int> c(123);
  cout<<f1(c)<<endl;
  
  C1<float>::nested n(3.5);
  cout<<n.W<<endl;
  cout<<f2(n)<<endl;  // CANNOT FIND THE CORRECT CODE FOR THIS
}


So, starting from version 3.4.2 it is no more possible to use a template
function that refers to a nested class of a template class.

g++ versions :
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.3 20041226 (Red Hat 3.4.3-11)

Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --with-gxx-include-dir=/usr/include/c++/3.4.3
--enable-languages=c,c++,java,f95 --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 4.0.0 20041228 (Red Hat 4.0.0-0.17)

-- 
           Summary: No more possible to have a template function that uses a
                    nested class of a template class
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jean-pierre dot chevallet at imag dot fr
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i386-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
@ 2005-01-06 10:13 ` giovannibajo at libero dot it
  2005-01-06 13:44 ` jean-pierre dot chevallet at imag dot fr
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: giovannibajo at libero dot it @ 2005-01-06 10:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2005-01-06 10:13 -------
This is invalid. Within the expression "C1<T>::nested", T cannot be deduced 
because it is in an nondeduced context. 

ISO/ANSI C++ standard reference: [temp.deduct.type]/4:

The nondeduced contexts are:

  --  The  nested-name-specifier  of  a  type that was specified using a
      qualified-id.

  --  A type that is a template-id in which one or more of the template-
      arguments is an expression that references a template-parameter.

  When  a  type  name  is  specified in a way that includes a nondeduced
  context, all of the types  that  comprise  that  type  name  are  also
  nondeduced.   However,  a  compound  type can include both deduced and
  nondeduced types.  [Example: If a type is  specified  as  A<T>::B<T2>,
  both  T  and  T2  are nondeduced.  Likewise, if a type is specified as
  A<I+J>::X<T>, I, J, and T are nondeduced.  If a type is  specified  as
  void f(typename A<T>::B, A<T>), the T in A<T>::B is nondeduced but the
  T in A<T> is deduced.


Also notice the end of paragraph 3: "If a template parameter is used only in 
nondeduced contexts and is not explicitly specified, template argument 
deduction fails."


Using a specialization as a workaround is a bit bloating. The correct way is to 
explicitly specify the template arguments which cannot be deduced when calling 
the template. In your case, you can use f2<float>(n).

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
  2005-01-06 10:13 ` [Bug c++/19288] " giovannibajo at libero dot it
@ 2005-01-06 13:44 ` jean-pierre dot chevallet at imag dot fr
  2005-01-06 14:49 ` bangerth at dealii dot org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jean-pierre dot chevallet at imag dot fr @ 2005-01-06 13:44 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jean-pierre dot chevallet at imag dot fr  2005-01-06 13:44 -------
Subject: RE:  No more possible to have a template function that uses a nested class of a template class

Thank you very much for your answer, and I'm very impressed : you have
answer very quicky !!

May I ask you what follows my problem : instead of a normal function I
have an operator in the "real" code I try to compile.

The operator is << : but how to explicitly specify template argument
when the tamplate function is an operator ? 

Thank you again !

PS: in fact I'm little disappointed the way C++ evolve during last
years, I feel have more and more difficulties to make all the code I
have in charge compiling when new version of gcc are available,
specially for the templates. Any advice ?

******************************************
Jean-Pierre Chevallet IPAL-CNRS
Image Processing and Application Lab
Institute for Infocomm Research I2R
21 Heng Mui Keng Terrace
Singapore 119613
http://ipal.imag.fr
Jean-Pierre.Chevallet@imag.fr
Tel work: +65 6874 8526
Fax: +65 6775 5014
Tel home: +65 6256 6141


> -----Original Message-----
> From: giovannibajo at libero dot it [mailto:gcc-bugzilla@gcc.gnu.org]
> Sent: jeudi 6 janvier 2005 18:13
> To: jean-pierre.chevallet@imag.fr
> Subject: [Bug c++/19288] No more possible to have a template function
that
> uses a nested class of a template class
> 
> 
> ------- Additional Comments From giovannibajo at libero dot it
2005-01-06
> 10:13 -------
> This is invalid. Within the expression "C1<T>::nested", T cannot be
> deduced
> because it is in an nondeduced context.
> 
> ISO/ANSI C++ standard reference: [temp.deduct.type]/4:
> 
> The nondeduced contexts are:
> 
>   --  The  nested-name-specifier  of  a  type that was specified using
a
>       qualified-id.
> 
>   --  A type that is a template-id in which one or more of the
template-
>       arguments is an expression that references a template-parameter.
> 
>   When  a  type  name  is  specified in a way that includes a
nondeduced
>   context, all of the types  that  comprise  that  type  name  are
also
>   nondeduced.   However,  a  compound  type can include both deduced
and
>   nondeduced types.  [Example: If a type is  specified  as
A<T>::B<T2>,
>   both  T  and  T2  are nondeduced.  Likewise, if a type is specified
as
>   A<I+J>::X<T>, I, J, and T are nondeduced.  If a type is  specified
as
>   void f(typename A<T>::B, A<T>), the T in A<T>::B is nondeduced but
the
>   T in A<T> is deduced.
> 
> 
> Also notice the end of paragraph 3: "If a template parameter is used
only
> in
> nondeduced contexts and is not explicitly specified, template argument
> deduction fails."
> 
> 
> Using a specialization as a workaround is a bit bloating. The correct
way
> is to
> explicitly specify the template arguments which cannot be deduced when
> calling
> the template. In your case, you can use f2<float>(n).
> 
> --
>            What    |Removed                     |Added
>
------------------------------------------------------------------------
--
> --
>              Status|UNCONFIRMED                 |RESOLVED
>          Resolution|                            |INVALID
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288
> 
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.



-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
  2005-01-06 10:13 ` [Bug c++/19288] " giovannibajo at libero dot it
  2005-01-06 13:44 ` jean-pierre dot chevallet at imag dot fr
@ 2005-01-06 14:49 ` bangerth at dealii dot org
  2005-01-06 15:31 ` giovannibajo at libero dot it
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: bangerth at dealii dot org @ 2005-01-06 14:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2005-01-06 14:46 -------
Giving explicit template arguments for template operators works 
the same way: write 
  x.operator<< <float> (abc) 
instead of 
  x << abc 
 
As for the evolution of C++: it isn't C++ but rather gcc that is evolving. 
The standard for C++ was finished in 1998, and since then gcc has just tried 
to be more standards conformant. gcc3.4 was a big step forward in this 
respect. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (2 preceding siblings ...)
  2005-01-06 14:49 ` bangerth at dealii dot org
@ 2005-01-06 15:31 ` giovannibajo at libero dot it
  2005-01-06 17:41 ` bangerth at dealii dot org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: giovannibajo at libero dot it @ 2005-01-06 15:31 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2005-01-06 15:31 -------
My passionate suggestion is to avoid nested classes, but you didn't hear this 
from me ;)

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (3 preceding siblings ...)
  2005-01-06 15:31 ` giovannibajo at libero dot it
@ 2005-01-06 17:41 ` bangerth at dealii dot org
  2005-01-10  3:04 ` jean-pierre dot chevallet at imag dot fr
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: bangerth at dealii dot org @ 2005-01-06 17:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2005-01-06 17:41 -------
Nah, that may be your Eurocentric viewpoint. Over here in Texas one 
thinks differently :-)) 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (4 preceding siblings ...)
  2005-01-06 17:41 ` bangerth at dealii dot org
@ 2005-01-10  3:04 ` jean-pierre dot chevallet at imag dot fr
  2005-01-10  4:21 ` bangerth at dealii dot org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jean-pierre dot chevallet at imag dot fr @ 2005-01-10  3:04 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jean-pierre dot chevallet at imag dot fr  2005-01-10 03:04 -------
(In reply to comment #3)
> Giving explicit template arguments for template operators works 
> the same way: write 
>   x.operator<< <float> (abc) 
> instead of 
>   x << abc 
>  

Ok, I have test it but with a function operator : it doesn't seems to work.
So what is wrong ??

#include <iostream>

using namespace std;

template <typename T> class C1 {
public:
  T V;
  C1(T v) : V(v) {};
  
  class nested {
  public:
    T W;
    nested(T w) : W(w) {};
  };
  
};

// Template version of operator using nested temptate class
template <typename T>
ostream&  operator << (ostream& out,typename C1<T>::nested& c) {return out<<c.W; }

// Non template version of operator using nested temptate class
// ostream&  operator << (ostream& out, C1<float>::nested& c) {return out<<c.W;}

int main() {
 
  C1<float>::nested n(3.5);
  cout<<n.W<<endl;;

  // Tested with gcc 3.3.2

  // cout<<n<<endl;;       
  // Works with non template, doesn't work with template version

  // operator<<(cout,n)<<endl;; 
  // Works with non template, doesn't work with template version
  
  //cout.operator<< <float> (n);
  // Never works : because << is a function and not a member function

  //operator<< <float> (cout,n);
  // Never work !! but it should ???
}

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (5 preceding siblings ...)
  2005-01-10  3:04 ` jean-pierre dot chevallet at imag dot fr
@ 2005-01-10  4:21 ` bangerth at dealii dot org
  2005-01-10  4:39 ` gdr at integrable-solutions dot net
  2005-01-10  5:56 ` jean-pierre dot chevallet at imag dot fr
  8 siblings, 0 replies; 10+ messages in thread
From: bangerth at dealii dot org @ 2005-01-10  4:21 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2005-01-10 04:21 -------
You are trying to overload operator<<(std::ostream, float). That 
can't work, since there is already an overload for that. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (6 preceding siblings ...)
  2005-01-10  4:21 ` bangerth at dealii dot org
@ 2005-01-10  4:39 ` gdr at integrable-solutions dot net
  2005-01-10  5:56 ` jean-pierre dot chevallet at imag dot fr
  8 siblings, 0 replies; 10+ messages in thread
From: gdr at integrable-solutions dot net @ 2005-01-10  4:39 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gdr at integrable-solutions dot net  2005-01-10 04:39 -------
Subject: Re:  No more possible to have a template function that uses a nested class of a template class

"jean-pierre dot chevallet at imag dot fr" <gcc-bugzilla@gcc.gnu.org> writes:

| > Giving explicit template arguments for template operators works 
| > the same way: write 
| >   x.operator<< <float> (abc) 
| > instead of 
| >   x << abc 
| >  
| 
| Ok, I have test it but with a function operator : it doesn't seems to work.
| So what is wrong ??
| 
| #include <iostream>
| 
| using namespace std;
| 
| template <typename T> class C1 {
| public:
|   T V;
|   C1(T v) : V(v) {};
|   
|   class nested {
|   public:
|     T W;
|     nested(T w) : W(w) {};
|   };
|   
| };
| 
| // Template version of operator using nested temptate class
| template <typename T>
| ostream&  operator << (ostream& out,typename C1<T>::nested& c) {return out<<c.W; }

This template is never going to contribute any candidate through the
usual template argument deduction process.  The reason is very simple:
the template parameter "T" appears in nondeducible context.

| // Non template version of operator using nested temptate class
| // ostream&  operator << (ostream& out, C1<float>::nested& c) {return out<<c.W;}

this function does not involve any template deduction, so it will "work".

[...]

|   //operator<< <float> (cout,n);
|   // Never work !! but it should ???

Yes.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/19288] No more possible to have a template function that uses a nested class of a template class
  2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
                   ` (7 preceding siblings ...)
  2005-01-10  4:39 ` gdr at integrable-solutions dot net
@ 2005-01-10  5:56 ` jean-pierre dot chevallet at imag dot fr
  8 siblings, 0 replies; 10+ messages in thread
From: jean-pierre dot chevallet at imag dot fr @ 2005-01-10  5:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jean-pierre dot chevallet at imag dot fr  2005-01-10 05:56 -------
(In reply to comment #7)
> You are trying to overload operator<<(std::ostream, float). That 
> can't work, since there is already an overload for that. 
>  
> W. 

I don't think so, I try to define un fonction like this :

operator<<(std::ostream, C1<float>::nested&) with is not an overload of
operator<<(std::ostream, float).

The lesson I learn from all your replies : 
nested class of a template wich have no template
parametrer are difficult to use due to the notion nondeducible context.
I think it is better to avoid non template nested class, or adding a
explicit template parameter to the nested class, like :

template <typename T> class C1 {
public:
  T V;
  C1(T v) : V(v) {};
  
  template <typename U>
  class nested {
  public:
    U W;
    nested(U w) : W(w) {};
  };  
};
// Template version of operator using nested temptate class
template <typename T>
ostream&  operator << (ostream& out,typename C1<T>::nested<T>& c) {return
out<<c.W; }

  C1<float>::nested<float> n(3.5);

  cout<<n<<endl; // Here find the correct instantiation

or not use nested template class at all ........




-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19288


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-01-10  5:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-06  8:40 [Bug c++/19288] New: No more possible to have a template function that uses a nested class of a template class jean-pierre dot chevallet at imag dot fr
2005-01-06 10:13 ` [Bug c++/19288] " giovannibajo at libero dot it
2005-01-06 13:44 ` jean-pierre dot chevallet at imag dot fr
2005-01-06 14:49 ` bangerth at dealii dot org
2005-01-06 15:31 ` giovannibajo at libero dot it
2005-01-06 17:41 ` bangerth at dealii dot org
2005-01-10  3:04 ` jean-pierre dot chevallet at imag dot fr
2005-01-10  4:21 ` bangerth at dealii dot org
2005-01-10  4:39 ` gdr at integrable-solutions dot net
2005-01-10  5:56 ` jean-pierre dot chevallet at imag dot fr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).