public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55778] New: Variadic template extension possibly wrong
@ 2012-12-21 17:46 koala01 at free dot fr
  2012-12-21 18:38 ` [Bug c++/55778] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: koala01 at free dot fr @ 2012-12-21 17:46 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 55778
           Summary: Variadic template extension possibly wrong
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: koala01@free.fr


with the code
#include <string>
#include <iostream>
template<typename ...Args>
void foo(Args ... args)
{
    std::cout<<"stopping with "<<sizeof...(args)<< " unused
arguments"<<std::endl;
}
template<typename ...Args>
void foo(int i, Args ... args)
{
    std::cout<<"using i = "<<i
             <<", left "<<sizeof...(args)<<" argument more"<<std::endl;
    foo(args...);
}
template<typename ... Args>
void foo(std::string const & s, Args...args)
{
    std::cout<<"using the "<<s<<" string, left "
             <<sizeof...(args)<<" argument more"<<std::endl;
    foo(args...);
}
int main()
{

    foo<int, int,int, int, std::string , int, int >(1, 2, 3, 4,5 ,
                                                   "hello", 6, 7 );
    return 0;
}

we get a result like
using i = 1, left 7 argument more
using i = 2, left 6 argument more
using i = 3, left 5 argument more
using i = 4, left 4 argument more
using i = 5, left 3 argument more
stopping with 3 unused arguments

but we could expect a result like 

using i = 1, left 7 argument more
using i = 2, left 6 argument more
using i = 3, left 5 argument more
using i = 4, left 4 argument more
using i = 5, left 3 argument more
using the hello string, left 2 argument more
using i = 6, left 1 argument more
using i = 7, left 0 argument more
stopping with 0 unused arguments

just like with the code 

#include <iostream>
#include <string>
template<typename ...Args>
void foo(int i, Args ... args);
template<typename ...Args>
void foo(std::string const & s, Args...args);
template<typename ...Args>
void foo(Args ... args)
{
    std::cout<<"stopping with "<<sizeof...(args)<< " unused
arguments"<<std::endl;
}
template<typename ... Args>
void foo(std::string const & s, Args...args)
{
    std::cout<<"using the "<<s<<" string, left "
             <<sizeof...(args)<<" argument more"<<std::endl;
    foo(args...);
}
template<typename ...Args>
void foo(int i, Args ... args)
{
    std::cout<<"using i = "<<i
             <<", left "<<sizeof...(args)<<" argument more"<<std::endl;
    foo(args...);
}
int main()
{

    foo<int, int,int, int, std::string , int, int >(1, 2, 3, 4,5 ,
                                                   "hello", 6, 7 );
    return 0;
}

Am i wrong ?


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

* [Bug c++/55778] Variadic template extension possibly wrong
  2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
@ 2012-12-21 18:38 ` redi at gcc dot gnu.org
  2012-12-21 19:17 ` koala01 at free dot fr
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-21 18:38 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-21 18:37:54 UTC ---
(In reply to comment #0)
> Am i wrong ?

Yes.  The foo(std::string const&, Args...) overload is not in scope within
foo(int, Args...) so the call resolves to the foo(Args...) overload.


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

* [Bug c++/55778] Variadic template extension possibly wrong
  2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
  2012-12-21 18:38 ` [Bug c++/55778] " redi at gcc dot gnu.org
@ 2012-12-21 19:17 ` koala01 at free dot fr
  2012-12-21 19:43 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: koala01 at free dot fr @ 2012-12-21 19:17 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #2 from ph dunski <koala01 at free dot fr> 2012-12-21 19:17:05 UTC ---
(In reply to comment #1)
> (In reply to comment #0)
> > Am i wrong ?
> 
> Yes.  The foo(std::string const&, Args...) overload is not in scope within
> foo(int, Args...) so the call resolves to the foo(Args...) overload.

If you say so ...

But in this case, how is it possible to have a diffrent behavior simply by
switching foo (const std :: string &, Args ..) and foo (int, args ...)
implemented?

If you just try to compile my first test code, you will see that the execution
stops before writing the string, 3 more.

If you go foo (std :: string const &, Args ..) and foo (int, args ...)
implemented in the code, you will see that the execution stops after writing
the string, with argument 2 more.

I've always heard that the implementation order should not have an impact on
the final result.

Am i rong again?


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

* [Bug c++/55778] Variadic template extension possibly wrong
  2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
  2012-12-21 18:38 ` [Bug c++/55778] " redi at gcc dot gnu.org
  2012-12-21 19:17 ` koala01 at free dot fr
@ 2012-12-21 19:43 ` redi at gcc dot gnu.org
  2012-12-21 19:57 ` koala01 at free dot fr
  2012-12-21 21:05 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-21 19:43 UTC (permalink / raw)
  To: gcc-bugs


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-21 19:42:45 UTC ---
(In reply to comment #2)
> I've always heard that the implementation order should not have an impact on
> the final result.
> 
> Am i rong again?

The problem isn't the order the function definitions (i.e. implementations)
appear in, it's the order they are declared in. A function that hasn't been
declared yet can't be called, that's pretty basic:

int f()
{
  return g(); // error
}

int g() { return 0; }

To make you code compile just declare the foo(const std::string&, Args...)
overload before any code needs to call it, so add its declaration before the
definition of foo(int, Args...)


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

* [Bug c++/55778] Variadic template extension possibly wrong
  2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
                   ` (2 preceding siblings ...)
  2012-12-21 19:43 ` redi at gcc dot gnu.org
@ 2012-12-21 19:57 ` koala01 at free dot fr
  2012-12-21 21:05 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: koala01 at free dot fr @ 2012-12-21 19:57 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #4 from ph dunski <koala01 at free dot fr> 2012-12-21 19:57:05 UTC ---
It is what i did ;)

But, i'm really not convicted, because, in my head,  we should have a SFINAE
behaviour which should fall back into the good overloaded version until there
are no more argument :P

Maybe i just forget something from the norm, but it would be coherent to get
this behaviour, no ???


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

* [Bug c++/55778] Variadic template extension possibly wrong
  2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
                   ` (3 preceding siblings ...)
  2012-12-21 19:57 ` koala01 at free dot fr
@ 2012-12-21 21:05 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-21 21:05 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-21 21:05:36 UTC ---
No. SFINAE only applies if there's a substitution error, which doesn't apply
here. The foo(Args...) overload is viable and deduction succeeds unless the
argument types are not copyable.  To make the other overload viable it must be
declared. Clang gives exactly the same result, this is not a compiler bug. If
you think the language has a defect this is the wrong place to discuss it.


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

end of thread, other threads:[~2012-12-21 21:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-21 17:46 [Bug c++/55778] New: Variadic template extension possibly wrong koala01 at free dot fr
2012-12-21 18:38 ` [Bug c++/55778] " redi at gcc dot gnu.org
2012-12-21 19:17 ` koala01 at free dot fr
2012-12-21 19:43 ` redi at gcc dot gnu.org
2012-12-21 19:57 ` koala01 at free dot fr
2012-12-21 21:05 ` redi at gcc dot gnu.org

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).