Bug 41703 - Problems with SFINAE. Source works at gcc 3.4.6 but fails at 4.2.1 and 4.5.0.20091008
Summary: Problems with SFINAE. Source works at gcc 3.4.6 but fails at 4.2.1 and 4.5.0....
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: 4.5.0
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-14 05:51 UTC by DimanNe
Modified: 2009-11-06 04:10 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.2 4.4.1
Last reconfirmed: 2009-11-05 18:41:37


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description DimanNe 2009-10-14 05:51:47 UTC
Hello! I have FreeBSD 8.0-BETA4.
Why this

#include <iostream>
template <typename T, unsigned int (T::*)() const>
class TSizeEnabler { public: typedef T TClass; };

template <typename X> unsigned int GetAllSize(const X &Var)
   { return sizeof(Var); }
template <typename X> unsigned int GetAllSize(const typename TSizeEnabler<X, &X::CalcMySize>::TClass &Var)
   { return Var.CalcMySize(); }

template <typename T>
class THash
   {
      public:
      T Var;
      unsigned int CalcMySize() const { return 42 + GetAllSize<T>(Var); }
   };
int main()
   {
      int a;
      THash<int> b;
      std::cout << GetAllSize<int>(a) << std::endl;
      std::cout << GetAllSize< THash<int> >(b) << std::endl;
      return 0;
   }


works well at gcc 3.4.6 but fails at gcc 4.2.1 and gcc 4.5.0.20091008 with these messages:
gcc 4.5.0.2009100 output:
%g++45 -std=c++0x main.cpp
main.cpp: In function 'int main()':
main.cpp:24:46: error: call of overloaded 'GetAllSize(THash<int>&)' is ambiguous
main.cpp:6:36: note: candidates are: unsigned int GetAllSize(const X&) [with X = THash<int>]
main.cpp:8:36: note: unsigned int GetAllSize(const typename TSizeEnabler<X, (& X::CalcMySize)>::TClass&) [with X = THash<int>, typename TSizeEnabler<X, (& X::CalcMySize)>::TClass = THash<int>]
%
And gcc 4.2.1 output:
%g++ main.cpp
main.cpp: In function 'int main()':
main.cpp:23: error: call of overloaded 'GetAllSize(THash<int>&)' is ambiguous
main.cpp:5: note: candidates are: unsigned int GetAllSize(const X&) [with X = THash<int>]
main.cpp:7: note:                 unsigned int GetAllSize(const typename TSizeEnabler<X, (& X::CalcMySize)>::TClass&) [with X = THash<int>]
%


And how i can fix it the problem?
Thanks.
Comment 1 Jonathan Wakely 2009-10-14 09:10:59 UTC
Slightly reduced:

template <typename T, int (T::*)() const>
struct TSizeEnabler
{
    typedef T TClass;
};

template <typename X>
int
GetAllSize(const X &Var)
{ return sizeof(Var); }

template <typename X>
int
GetAllSize(const typename TSizeEnabler<X, &X::func>::TClass &Var)
{ return Var.func(); }

struct H
{
    int func() const;
};

int main()
{
    H b;
    return GetAllSize< H >(b);
}

I think you're right that this is a bug.  As a workaround you might be able to remove GetAllSize(const X &Var) from the overload set for the case when X::CalcMySize exists.
Comment 2 Jason Merrill 2009-11-06 03:33:22 UTC
Subject: Bug 41703

Author: jason
Date: Fri Nov  6 03:32:55 2009
New Revision: 153957

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=153957
Log:
	PR c++/41703
	* pt.c (check_undeduced_parms): New subroutine of...
	(more_specialized_fn): ...here.  Undeduced template parms can make
	a template less specialized than another.

Added:
    trunk/gcc/testsuite/g++.dg/template/partial6.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/ChangeLog

Comment 3 Jason Merrill 2009-11-06 04:10:43 UTC
Fixed for 4.5.