f



cv-qualified function type and type deduction

Good day!
I came across an interesting feature in the behavior of popular
compilers and would like to understand the reasons.
There's a simple code:

#include <iostream>

template <typename F>
void (* declval() ) (F);

template <typename X> 
char (& probe( void(*) ( X * ) ) )[1];

template <typename X> 
char (& probe( void(*) ( X   ) ) )[2];

int main()
{
    std::cout << sizeof( probe(declval<void() const>()) );
}

The question is about type deduction. If you run this code on
* compiler GCC 4.8.x (or later)
* compiler Clang 3.4 (or later)
* compiler in the Visual Stuido (tested on VS 2013 Community)
* compiler Intel ICC 13.0.1

it will display the number "1". And I thought it is logical, since
function type, being substituted argument in another function, acquires
the properties of a pointer type.
On newer versions of compilers mentioned (except for VS and ICC)
behavior has changed and now this code for Clang 3.5 (or higher), GCC
4.9 (or higher) displays the number "2". If you remove the second
overload, then the mentioned versions of GCC and Clang error occurs:
"Forming pointer to qualified function type"
I understand why but doubt whether this rule applies in this context.

In particular, it would be interesting to know whether the behavior of
the new and old versions of the compilers conforms to C++ Standard.

Maybe together we can find the truth. 
Thank you.


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

0
wander
2/20/2016 7:15:53 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
472 Views

Similar Articles

[PageSpeed] 12

Am 20.02.2016 um 14:15 schrieb wander:
>
> Good day!
> I came across an interesting feature in the behavior of popular
> compilers and would like to understand the reasons.
> There's a simple code:
>
> #include <iostream>
>
> template <typename F>
> void (* declval() ) (F);
>
> template <typename X>
> char (& probe( void(*) ( X * ) ) )[1];
>
> template <typename X>
> char (& probe( void(*) ( X   ) ) )[2];
>
> int main()
> {
>      std::cout << sizeof( probe(declval<void() const>()) );
> }
>
> The question is about type deduction. If you run this code on
> * compiler GCC 4.8.x (or later)
> * compiler Clang 3.4 (or later)
> * compiler in the Visual Stuido (tested on VS 2013 Community)
> * compiler Intel ICC 13.0.1
>
> it will display the number "1". And I thought it is logical, since
> function type, being substituted argument in another function,
acquires
> the properties of a pointer type.
> On newer versions of compilers mentioned (except for VS and ICC)
> behavior has changed and now this code for Clang 3.5 (or higher), GCC
> 4.9 (or higher) displays the number "2". If you remove the second
> overload, then the mentioned versions of GCC and Clang error occurs:
> "Forming pointer to qualified function type"
> I understand why but doubt whether this rule applies in this context.
>
> In particular, it would be interesting to know whether the behavior of
> the new and old versions of the compilers conforms to C++ Standard.

My interpretation of the code is that it should be required to be 
ill-formed after acceptance of

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

because it is not valid to form a pointer to a function with 
cv-qualifier-seq (In the example: 'void() const'). It is somewhat 
interesting to note that this interpretation also depends on the 
semantics of function parameter decay which is also discussed by

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

But even if the details of CWG 1668 are not clarified yet, I believe it 
is save to assume that the transformation mentioned in [dcl.fct] p5

"After determining the type of each parameter, any parameter of type 
�array of T� or of function type T is adjusted to be �pointer to T�.

would produce an invalid type and would require a diagnostics, because 
the function type is 'void() const'. The wording details of CWG 1668 
will hopefully clarify further details, e.g. whether the substituation 
attempt for dependent function parameters will effectively lead to 
SFINAE, but regardless of that detail, the presented example doesn't 
provide any third valid fall-back for 'probe' that could be selected, so

there should be no function probe that could be selected by overload 
resolution.

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! ]

0
UTF
2/22/2016 7:49:40 PM
Reply:

Similar Artilces:

mapping of XML types to C/C++ built-in types
Hello, Does W3C defines a standard for mapping XML types to C/C++ built-in types and vise versa? Thank you, Alona Hi Alona, "Alona" <allab@sympatico.ca> writes: > Does W3C defines a standard for mapping XML types to C/C++ built-in > types and vise versa? I don't think there is such a thing as XML type. Schema languages (e.g., W3C XML Schema) normally define primitive types. Some of them can be mapped to C/C++ fundamental types but I don't think W3C defines such a mapping. One proprietary mapping of XML Schema built-in types to C++ types is described in this...

Argument type of function and safe types and types, arguments can handle safely
Groups, I have few doubts regarding argument type of function and types, arguments can handle safely. I know these issues have been discussed at multiple places on internet. I wanted to see the big picture and needed opinion of C++ gurus When you pass by value - Copy constructor is called (except for basic data type like int, float). you can pass the **same type variable** only When you pass by reference - A reference will be intialized with pass value. you can pass **non-const same type or derived type** (passing a constant type will return in error) When you pass by constant referen...

What is the purpose of type() and the types module and what is a type?
The type() builtin according to python docs, returns a "type object". http://docs.python.org/2/library/types.html And in this module is bunch of what I assume are "type objects". Is this correct? http://docs.python.org/2/library/functions.html#type And type(), aside from being used in as an alternative to a class statement to create a new type, really just returns the object class, doesn't it? >>> import types >>> a = type(1) >>> b = (1).__class__ >>> c = int >>> d = types.IntType >>> a is b is c is...

C++ typed interface to hundreds of C functions
Hi there, I'm handling pointers to "objects" described by C-style struct (e.g. Base), and I manipulate them through a low level C API. Sometimes a third-party binary function is applied to two of these, giving a pointer to a third. extern "C" Base *foo1_A1_B1_C1(Base *, Base *); extern "C" Base *foo1_A1_B1_C2(Base *, Base *); extern "C" Base *foo1_A1_B2_C1(Base *, Base *); ... etc. (hundreds more) I'd like to provide a better interface to this using class templates for the A,B and C parts: Derived<A,B,C> *foo1(Derived<A,B,C> &a...

What differences are for type qualifier and type casting?
Hi, I see the following on a tutorial, but it is not clear about the difference= s=20 between type qualifier and type casting. How do I use either one from "1010" and B_slv? Thanks, =E2=97=8F VHDL type qualifier (type_name') is a mechanism that specifies th= e type of an operand or return value of a subprogram (or operator). Z_sv <=3D A_sv + signed'("1010") ;=20 =E2=97=8F Without ', it is type casting. Use type casting for: Z_sv <=3D A_sv + signed(B_slv) ;=20 On Monday, September 28, 2015 at 9:41:21 AM UTC-4, fl wrote: > ...

Type conversion function for user type again.
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b), x1(c), y1(d) {} void Union(const DRect* p) { x0 = MIN(x0, p->x0); y0 = MIN(y0, p->y0); x1 = MAX(x1, p->x1); y1 = MAX(y1, p->y1); } }; class IRect { private : int x0, y0, x1, y1; public : IRect(int a, int b, int c, int d) : x0(a), y0(b), x1(c), y1(d) {} opera...

Deduction of not-type template parameter type
Sometimes it would be convenient to allow a function argument to be used as a template argument. For example, consider a factory function that generates lookup functions (the example is inspired by James Kanze's thread "Incomplete type in template parameter, complete type as argument"): typedef bool (*contains_fun) (const char* key); template <size_t N, const char* (&Keys) [N]> bool contains (const char* key) { return std::find_if (Keys, Keys + N, Match (key)) != Keys + N; } template <size_t N> contains_fun make_contains ...

file types/data types/mime types
I am puzzled about something with osX. Why does it use the file extension for determining file type instead of looking into the file for the type? My Amiga used datatypes 15 years ago to determine what kind of file it was. If it was jpeg but named .gif, no problem. The jpeg program loaded it. For instance, I had a file on my desktop, an html file of instructions. It had no file extension so appeared on the desktop as a blank white rectangle. Re-named it 'html' so firefox showed up on the icon. I just renamed it 'jpg' and GraphicConverter tried to open it. It reported &q...

STD types vs C++ intrinsic types
I have been reading a book that focuses on understanding the intrinsic types of C++ in depth. The author's mentality is this: "Understand the intrinsic types, then learn the std types as needed later", but I have been reading the stroustrup (spelling?) book and he says that it is much better to learn the standard library first as a beginner, and then worry about the intrinsic types afterwards. So there is no doubt that you need to have a full comprehension of both, but as a general rule, which should be relied upon more? For example, in many cases you can use an array to hold a ...

function overload with array types and pointer types
{ Please limit your text to fit within 80 columns, preferably around 70, so that readers don't have to scroll horizontally to read each line. This article has been reformatted manually by the moderator. -mod } I have the following code. ----------------- BEGIN ---------------- #include <cstddef> #include <type_traits> template <typename T0, typename T1> typename std::enable_if<sizeof(T0)==sizeof(T1)>::type inline f (T0 const* const t0, T1 const* const t1) { // do something to *t0 and *t1 } template <typename T0, typename T1, size_t N0,...

C++ objects are value type or reference type
I once think C++ objects are reference types, similar as C# objects. If a C++ object are passed to a function, a reference will be saved in the stack, and points to the object in the heap. However, in my test shown below, it seems that C++ objects are actually value type. "clTest2.MYTEST(clTest);" actually triggered "CTest(const CTest &)" to create an internal object. After ""clTest2.MYTEST(clTest);" is "executed,clTest.i" is "2", not "3". Can I say C++ objects are value type? Thank you! class CTest { public: i...

Type conversion function for user defined type...
I have a question about type conversion function for user defined type. I have two classes class DRect { private : double x0; double y0; double x1; double y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b), x1(c), y1(d) {} void Union(DRect* p) { x0 = MIN(x0, p->x0); y0 = MIN(y0, p->y0); x1 = MAX(x1, p->x1); y1 = MAX(y1, p->y1); } } class IRect { private : int x0; int y0; int x1; int y1; public : IRect(int a, int b, int c, int d) : x0(a), y0(b), x1(c), y1(d) {} } And I want to do something lik...

How to deal with calling a member function from a static callback C type function
Hi all I am using a C style function which requires a 'callback' function to be defined. This callback function is called whenever the interface requests new data. The function is a bit like this: create_object(<callback_function>) I have a class, MyClass, which uses this functionality so I have an Init() member function which calls create_object. However the callback_function cannot be a member function - so I make it a static. I want the callback_function to call a member function - but a static function contains no this pointer - so what to do? At present I have a glob...

[ANN] Typelib: a C/C++ type/value manipulation library, and dynamic function calls
Typelib 1.0rc1 is out ! Author: Sylvain Joyeux <sylvain.joyeux@m4x.org> Project page: http://sourceforge.net/projects/typelib Documentation: C++ library: http://typelib.sourceforge.net/html/ Ruby bindings: http://typelib.sourceforge.net/html/ruby Typelib is a C++ library which allows introspection on data types and data values. Its value model is based on the C type model. The library allows to load definition from various type description files (including plain C), build types programmatically, create and manipulate values defined using these types. The Ruby bindings to the ...

How to define a template function parameterized by type of function? Accelerated C++ exercise 8-1
It's from the Accelerated C++ exercise 8-1 The old version is based on pointer to function: double analysis(const std::vector<Student_info>& students, double analysis_grade(const Student_info& s)) { vector<double> grades; transform(students.begin(), students.end(), back_inserter(grades), analysis_grade); return median(grades); } void write_analysis(ostream& out, const string& name, double analysis(const std::vector<Student_info>& students, double analysis_grade(const Student_info& s)), double analysis_grade(const Student...

C.8.16.1 Image Type and Frame Type vs C.8.16.1.4 Derived Pixel Contrast
There is a small inconsistency in between =A7C.8.16.1 Image Type and Frame = Type and =A7C.8.16.1.4 Derived Pixel Contrast: [...] Image Type (0008,0008) and Frame Type (0008,9007) shall consist of four non= -zero length values. [...] while [...] Value 4 of Image Type (0008,0008) and Value 4 of Frame Type (0008,9007) sha= ll not be zero length unless the SOP Class UID is "1.2.840.10008.5.1.4.1.1.= 2.2" or "1.2.840.10008.5.1.4.1.1.4.4" or "1.2.840.10008.5.1.4.1.1.128.1" (L= egacy Converted). [...] Thanks Mathieu I will put in a CP about this. ...

type declaration in declaration of a parameter or return type of a function
hello is it strictly conforming to declare a type (struct, union or enum) in the declaration of a parameter or of the return type of a function? For instance: struct s { int a; float b; } f( int ) ; int g ( enum e { zero, one, two } x ) { enum e y; return x==y; } Luca Forlizzi wrote: > hello > > is it strictly conforming to declare a type (struct, union or enum) > in the declaration of a parameter > or of the return type of a function? > For instance: > struct s { int a; float b; } f( int ) ; > int g ( enum e { zero, one, two } x ) { enum...

cv qualified return types
Hi, I would like to know what should be the behavior of the compiler for the code shown const int fn(){return 0;} Comeau/Gcc give warning whereas C++ standard does not appear to mention anything specific. However looking at the grammar, it appears to me that such a declaration is syntactically legal. Regards, Dabs. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] On 27 Apr., 16:01, Anubhav <rkld...@gmail.com> wrote: > Hi, > > I would like to know what should be ...

SFINAE with invalid function-type or array-type parameters?
Please consider this code: // snip template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main() { char c[sizeof(f<void()>(0)) == 2]; } // snap I expected it doing SFINAE and chosing the second overload, since substitution of T into T[1] yields void [1]() Which is an invalid type, of course. Adjustment of parameter types (array->pointer) is done after substituting template parameters into function parameters and checking for valid resulting types like 14.8.2 [temp.deduct] describes. But both comeau and GCC fail to compile t...

Type A ,Type B
Hi, Someone please differentiate Type A and Type B messages/network? or give me poitners to where i can find them.. Thanks, ...Vinodh.. In article <1129109340.812999.298990@f14g2000cwb.googlegroups.com>, "Vinodh Saigopal" <vinodh.sai@gmail.com> wrote: > Hi, > > Someone please differentiate Type A and Type B messages/network? > or give me poitners to where i can find them.. What protocol are you talking about? There's no such terms, AFAIK, in IP, TCP, UDP, or ICMP. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PL...

Template function type for arithmetic operators for elementary types
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b; typedef int& optype(int&,int); // ??? template<optype OP> void element_wise(X& x) { OP(a,x.a); OP(b,x.b); } X& operator+=(X& x) { element_wise< ::operator+=<int> >(x); // ??? (line 15) return *this; } }; int main() { X x,y; x.a = 1; x.b = 2; y.a = 3; y.b = 4; x += y...

Array types and cv-qualifiers
I have read DR "Array types and cv-qualifiers": http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#112 But I still don't understand whether an array type can be cv- qualified. Lets consider the following examples: 1) Is the program below well-formed? template <class T> struct is_const { static bool const value = false; }; template <class T> struct is_const<T const> { static bool const value = true; }; template <class T> void f(T &) { char checker[is_const<T...

Template type deduction failure when using reference types
Hello all, Can anyone help me with a particularly obscure error? The following code fails to compile on g++ template<typename retT, typename argT> void func(retT (*funcPtr)(argT), argT argument) {} void func1(int a) {} void func2(int& a) {} int main() { int val = 1; int& valref = val; func(&func1, valref);//Succeeds func(&func2, valref);//Fails } The compiler appears to lose the ampersand from argT when it creates the function. I'm completely stuck on this one so any help would be greatly appreciated. Thanks, Marcus -- [ See...

Return-type deduction for all functions
Would it make sense to generalize the implicit return-type deduction of the lambda functions to all functions? Motivation: reading the Boost.Proto documentation and seeing something like this: template< int Exp, typename Arg > typename proto::result_of::make_expr< proto::tag::function // Tag type , pow_fun< Exp > // First child (by value) , Arg const & // Second child (by reference) >::type const pow(Arg const &arg) { return proto::make_expr<proto::tag::function>( pow_fun<Exp>() // First ch...

Web resources about - cv-qualified function type and type deduction - comp.lang.c++.moderated

Resources last updated: 2/20/2016 1:35:55 PM