Discussion:
Template argument deduction
(too old to reply)
Vladimir Grigoriev
2010-02-18 16:44:20 UTC
Permalink
Let consider the following template function

template <typename T>
const T & f( const T &x, const T &y )
{
std::cout << typeid( x ).name() << std::endl;
std::cout << typeid( y ).name() << std::endl;

return ( x );
}

When I run this function using Borland C++ compiler adding the statements

char s1[] = "ABCDE";
char s2[] = "GFHIJ";

std::cout << typeid( f( d1, s2 ) ).name() << std::endl;

I get the result

const char *
conts char *
const char *

When I run the code using Microsoft C++ 2005 EE I get

char const [6]
char const [6]
char const [6]

So it is interesting what should be displayed?

And if Microsoft C++ shows that the return value of f() is of type char
const [6] when how does look the corresponding instantiated function?

Vladimir Grigoriev


std::cout << typeid( f(
Igor Tandetnik
2010-02-18 17:13:19 UTC
Permalink
Post by Vladimir Grigoriev
Let consider the following template function
template <typename T>
const T & f( const T &x, const T &y )
{
std::cout << typeid( x ).name() << std::endl;
std::cout << typeid( y ).name() << std::endl;
return ( x );
}
When I run this function using Borland C++ compiler adding the
statements
char s1[] = "ABCDE";
char s2[] = "GFHIJ";
std::cout << typeid( f( d1, s2 ) ).name() << std::endl;
I get the result
const char *
conts char *
const char *
When I run the code using Microsoft C++ 2005 EE I get
char const [6]
char const [6]
char const [6]
So it is interesting what should be displayed?
VC is correct, I believe.
Post by Vladimir Grigoriev
And if Microsoft C++ shows that the return value of f() is of type
char const [6] when how does look the corresponding instantiated
function?
Not char const[6] - char const (&)[6]. A function can't return an array, but it can return a reference to an array. However, when the argument of typeid operator is a reference, it produces the type_info object for the referent.

const char(&f(const char(&x)[6], const char(&y)[6]) )[6];
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Vladimir Grigoriev
2010-02-18 17:24:25 UTC
Permalink
Thank you, Igor, very much. I was trying to place [6] before the list of
parameters and not at the end.:)

Vkadimir Grigoriev
Post by Vladimir Grigoriev
Let consider the following template function
template <typename T>
const T & f( const T &x, const T &y )
{
std::cout << typeid( x ).name() << std::endl;
std::cout << typeid( y ).name() << std::endl;
return ( x );
}
When I run this function using Borland C++ compiler adding the
statements
char s1[] = "ABCDE";
char s2[] = "GFHIJ";
std::cout << typeid( f( d1, s2 ) ).name() << std::endl;
I get the result
const char *
conts char *
const char *
When I run the code using Microsoft C++ 2005 EE I get
char const [6]
char const [6]
char const [6]
So it is interesting what should be displayed?
VC is correct, I believe.
Post by Vladimir Grigoriev
And if Microsoft C++ shows that the return value of f() is of type
char const [6] when how does look the corresponding instantiated
function?
Not char const[6] - char const (&)[6]. A function can't return an array, but
it can return a reference to an array. However, when the argument of typeid
operator is a reference, it produces the type_info object for the referent.

const char(&f(const char(&x)[6], const char(&y)[6]) )[6];
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925
Alex Blekhman
2010-02-19 08:58:05 UTC
Permalink
Post by Vladimir Grigoriev
Thank you, Igor, very much. I was trying to place [6] before the
list of parameters and not at the end.:)
The common way to cope with such nasty declarations is to use
`typedef' keyword:

typedef char my_buff_t[6];

const my_buff_t& f(const my_buff_t& x, const my_buff_t& y);

This way you can pack the syntactic ugliness into nice descriptive
type names.

HTH
Alex
Vladimir Grigoriev
2010-02-19 13:49:43 UTC
Permalink
Thanks Alex. I was trying to do the same but without using typedefs. So I
got errors.

Vladimir Grigoriev
Post by Vladimir Grigoriev
Thank you, Igor, very much. I was trying to place [6] before the list of
parameters and not at the end.:)
The common way to cope with such nasty declarations is to use `typedef'
typedef char my_buff_t[6];
const my_buff_t& f(const my_buff_t& x, const my_buff_t& y);
This way you can pack the syntactic ugliness into nice descriptive type
names.
HTH
Alex
Continue reading on narkive:
Loading...