From: Jesse Perla on
Hi,
It looks to me that when the compiler (VC 2010 here) tries to match a
function overload that it does a template expansion of the return
type. Am I missing anything?
Checkout the simplified problem I am having which will not compile:

struct tag1{};
struct tag2{};

struct data1{
typedef double value;
};

struct data2{ //Doesn't have a ::value member
};

template<typename T>
struct my_meta1
{
typedef typename T::value value;
};

template<typename T>
struct my_meta2
{
typedef double value;
};

//Matching the function based on the tag type
template<typename DataType>
typename my_meta1<DataType>::value
f(tag1, DataType data)
{
return 0.0;
}

template<typename DataType>
typename my_meta2<DataType>::value
f(tag2, DataType data)
{
return 0.0;
}


int main()
{
f(tag1(), data1()); //Should match the tag1 specialization, which in
turn gets the metadata from the data1() struct
f(tag2(), data2()); //Should match the tag2 specialization, which
doesn't need to call anything on data2()\
}

////////////
Am I missing anything here? Are there any work


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jesse Perla on
On Apr 19, 8:02 am, Mickey <jyoti.mic...(a)gmail.com> wrote:
> What kind of compiler error are you getting?
> I was able to compile without any warnings/error using g++.exe (GCC)
> 3.4.5 with -pedantic -Wall flags.

Thanks for looking at this.
1) In Intel 11.1 Windows I get the following:

error: class "data2" has no member "value"
typedef typename T::value value;
^
detected during instantiation of class "my_meta1<T> [with T=data2]"
at line 40

2) With MSVC2010, I get something equivalent.

3) And, sure enough, in MinGW with G++ 4.4.1, I get no errors!

I wonder whether this should actually compile or not. Can you think
of any workarounds?
(I tried something with boost::mpl::eval_if and mpl::lambda, but gave
up. It REALLY wants to instantiate that template)


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
Jesse Perla <jesseperla(a)gmail.com> writes:
<snip intro />

Note: I have added some key line numbers to your code below to match my
own build attempts.

> struct tag1{};
> struct tag2{};
>
> struct data1{
> typedef double value;
> };
>
> struct data2{ //Doesn't have a ::value member // line: 8
> };
>
> template<typename T>
> struct my_meta1
> {
> typedef typename T::value value; // line: 14
> };
>
> template<typename T>
> struct my_meta2
> {
> typedef double value;
> };
>
> //Matching the function based on the tag type
> template<typename DataType>
> typename my_meta1<DataType>::value
> f(tag1, DataType data)
> {
> return 0.0;
> }
>
> template<typename DataType>
> typename my_meta2<DataType>::value
> f(tag2, DataType data)
> {
> return 0.0;
> }
>
>
> int main()
> {
> f(tag1(), data1());
>
> f(tag2(), data2()); // line: 43
>
> }
>

To begin with, I should note that this code, as-is, builds without issue
using gcc-4.5.0 (as it clearly did for Mickey using gcc-3.4.5).

20:41:35 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $gcc -Wall -ansi -pedantic -c
template_expansion.cpp

21:02:19 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $

When I try to build the same code with VC2008, however, I get a string
of errors. You don't state what actual error messages you get for
VC2010, so they may not be the same. What I get is:

d:\CPPProjects\CLCPPM>cl /EHsc -c template_expansion.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

template_expansion.cpp
template_expansion.cpp(14) : error C2039: 'value' : is not a member
of 'data2'
template_expansion.cpp(8) : see declaration of 'data2'
template_expansion.cpp(43) : see reference to class template
instantiation 'my_meta1<T>' being compiled
with
[
T=data2
]
template_expansion.cpp(14) : error C2146: syntax error : missing ';'
before identifier 'value'
template_expansion.cpp(14) : error C4430: missing type specifier -
int assumed.
Note: C++ does not support default-int
template_expansion.cpp(14) : error C2602: 'my_meta1<T>::value' is not
a member of a base class of 'my_meta1<T>'
with
[
T=data2
]
template_expansion.cpp(14) : see declaration of 'my_meta1<T>::value'
with
[
T=data2
]
template_expansion.cpp(14) : error C2868: 'my_meta1<T>::value' :
illegal syntax for using-declaration; expected qualified-name
with
[
T=data2
]

Although I'm not that familiar with VC and its error messages, it
appears to be wanting to signal an error originating in line 43 and
resulting in a failure to instatiate my_meta1<T> with T = data2, since
data2 has no member named value.

Line 43 is the call:

f(tag2(), data2());

Since the first parameter is of type tag2, the correct instantiation is
clearly your second definition of f, with a return type of
my_meta2<data2>::value. So, I cannot really understand why the compiler
is complaining about a failed instantiation of my_meta1<data2> in the
return type of your first f, which ultimately doesn't match in any case
on its first function call parameter.

I would have to go over the Standard in some detail to refresh myself on
the process involved here in deducing the proper instantiation, but I
cannot easily believe the VC is applying it correctly here. At a casual
glance, it appears to be a failure to apply SFINAE appropriately.

Regards

Paul Bibbings

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mickey on
On Apr 19, 9:47 am, Jesse Perla <jessepe...(a)gmail.com> wrote:
> Hi,
> It looks to me that when the compiler (VC 2010 here) tries to match a
> function overload that it does a template expansion of the return
> type. Am I missing anything?
> Checkout the simplified problem I am having which will not compile:
>
(code snipped)

What kind of compiler error are you getting?
I was able to compile without any warnings/error using g++.exe (GCC)
3.4.5 with -pedantic -Wall flags.

Regards,
Jyoti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jesse Perla on
On Apr 20, 2:35 am, Paul Bibbings <paul.bibbi...(a)gmail.com> wrote:
> I would have to go over the Standard in some detail to refresh myself on
> the process involved here in deducing the proper instantiation, but I
> cannot easily believe the VC is applying it correctly here. At a casual
> glance, it appears to be a failure to apply SFINAE appropriately.

That is my gut as well. I posted this for the intel guys if anyone
is keen to track what their engineers say:
http://software.intel.com/en-us/forums/showthread.php?t=73680


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]