From: Roman.Perepelitsa on
Is this program valid? Why? If yes then what it prints?

#include <iostream>
#include <ostream>

enum { value };

template <class T>
int f(T) { return 1; }

int f(int) { return 2; }

int main()
{
std::cout << f(value) << std::endl;
}

Roman Perepelitsa.

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

From: Daniel Krügler on
On 11 Apr., 19:34, "Roman.Perepeli...(a)gmail.com"
<Roman.Perepeli...(a)gmail.com> wrote:
> Is this program valid? Why? If yes then what it prints?
>
> #include <iostream>
> #include <ostream>
>
> enum { value };
>
> template <class T>
> int f(T) { return 1; }
>
> int f(int) { return 2; }
>
> int main()
> {
> std::cout << f(value) << std::endl;
>
> }

We have two problems here:

1) Does value have linkage?
2) If it does not, should that result in a compile-error
or should it be a (silent) type-deduction failure?

For (1), even with extended definition of "names-for-linkage-
purposes",
as described in

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#389

this case does not belong to the valid cases [It would be valid
according to the newer definition, if you would have written
typedef enum { value } SomeTypedefName;
instead]

Following this interpretation, the problematic point
is that 'value' does not have linkage. This is so,
because of [temp.arg.type]/2 says:

"A local type, a type with no linkage, an unnamed type
or a type compounded from any of these types shall
not be used as a template-argument for a template
type-parameter.[..]"

{The current draft has s shortened version of this:
"A type without linkage (3.5) shall not be used as a
template-argument for a template type-parameter."}

Whether this program is valid or not, is currently
still an open question - and (2) can currently not be
answered for your example - see:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#488

HTH & Greetings from Bremen,

Daniel Kr�gler


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

From: Greg Herlihy on
On Apr 11, 10:34 am, "Roman.Perepeli...(a)gmail.com"
<Roman.Perepeli...(a)gmail.com> wrote:
> Is this program valid? Why? If yes then what it prints?
>
> #include <iostream>
>
> enum { value };
>
> template <class T>
> int f(T) { return 1; }
>
> int f(int) { return 2; }
>
> int main()
> {
> std::cout << f(value) << std::endl;
> }

No, the program above is not valid because the best match for the f()
function call in main is a function template that cannot legally be
instantiated. In particular, the anonymous enumerated type that would
instantiate f() does not have external linkage.

One solution would be to give value's anonymous enumerated type a
name. Another solution would avoid instantiating the f() function
template - by making f(int) a better match for the f() function call
in main:

std::cout << f(int(value)) << "\n";

Greg


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

From: Looney on
On Apr 13, 4:13 am, Greg Herlihy <gre...(a)mac.com> wrote:
> On Apr 11, 10:34 am, "Roman.Perepeli...(a)gmail.com"
>
>
>
> <Roman.Perepeli...(a)gmail.com> wrote:
> > Is this program valid? Why? If yes then what it prints?
>
> > #include <iostream>
>
> > enum { value };
>
> > template <class T>
> > int f(T) { return 1; }
>
> > int f(int) { return 2; }
>
> > int main()
> > {
> > std::cout << f(value) << std::endl;
> > }
>
> No, the program above is not valid because the best match for the f()
> function call in main is a function template that cannot legally be
> instantiated. In particular, the anonymous enumerated type that would
> instantiate f() does not have external linkage.
>
> One solution would be to give value's anonymous enumerated type a
> name. Another solution would avoid instantiating the f() function
> template - by making f(int) a better match for the f() function call
> in main:
>
> std::cout << f(int(value)) << "\n";
>
> Greg

as much as greg is right i would like to add that you may find that
lots of compilers may just compile it, including visual C++ 2005.
Also like Greg pointed out the function template would be instantiated
to answer the programmer's prayers

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