f



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 child (by value)
        , boost::ref(arg)   // Second child (by reference)
      );
}

I wish I could do it like this:

template< int Exp, typename Arg >
auto pow(Arg const &arg)
{
      return proto::make_expr<proto::tag::function>(
          pow_fun<Exp>()    // First child (by value)
        , boost::ref(arg)   // Second child (by reference)
      );
}

decltype does not help much here either...

-- 
http://kaba.hilvi.org


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Kaba
8/9/2012 6:51:49 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

5 Replies
446 Views

Similar Articles

[PageSpeed] 55

On 09/08/2012 19:51, Kaba wrote:
> 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:
>
But the problem is that only a function declaration is needed in order
to call a function. How will the caller know what the return type is?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Francis
8/10/2012 6:19:25 PM
10.8.2012 21:19, Francis Glassborow kirjoitti:
> On 09/08/2012 19:51, Kaba wrote:
>> 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:
>>
> But the problem is that only a function declaration is needed in
> order to call a function. How will the caller know what the return
> type is?

Perhaps the exact type is not always important to the user, such as in
this Proto-example. What is important though is the underlying concept
the type implements. However, without support from the language the
concept would have to be looked up from the documentation; I could
live with that:) But something like this would be even nicer:

      auto[Grammar_Concept] f();

-- 
http://kaba.hilvi.org


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Kaba
8/10/2012 9:35:46 PM
On 10/08/2012 22:35, Kaba wrote:
> 10.8.2012 21:19, Francis Glassborow kirjoitti:
>> On 09/08/2012 19:51, Kaba wrote:
>>> 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:
>>>
>> But the problem is that only a function declaration is needed in
>> order to call a function. How will the caller know what the return
>> type is?

'caller' here refers to a programming element not a programmer.

> Perhaps the exact type is not always important to the user, such as
> in this Proto-example. What is important though is the underlying
> concept the type implements. However, without support from the
> language the concept would have to be looked up from the
> documentation; I could live with that:) But something like this
> would be even nicer:
>
>       auto[Grammar_Concept] f();
>

You are missing the point. Here is an example:

template<typename T> g(T t);

int main(){
     g(f());  // how is the compiler to deal with this?
// ...
}


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Francis
8/11/2012 6:14:08 AM
11.8.2012 9:14, Francis Glassborow kirjoitti:
> On 10/08/2012 22:35, Kaba wrote:
>> 10.8.2012 21:19, Francis Glassborow kirjoitti:
>>> On 09/08/2012 19:51, Kaba wrote:
>>>> 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:
>>>>
>>> But the problem is that only a function declaration is needed in
>>> order to call a function. How will the caller know what the return
>>> type is?
>
> template<typename T> g(T t);
>
> int main(){
>      g(f());  // how is the compiler to deal with this?
> // ...
> }

I see. Then one would need to add the requirement that a function with
the implicit-return type has to be defined before its return-type is
queried. In this case, emit "error: Unable to deduce the return-type of
the function template <typename T> g(T t), where T = <type of f()>".
Lambdas are able to avoid this problem because they can never be
forward-declared.

-- 
http://kaba.hilvi.org


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Kaba
8/11/2012 7:17:29 PM
On 8/10/12 5:35 PM, Kaba wrote:
> 10.8.2012 21:19, Francis Glassborow kirjoitti:
>> On 09/08/2012 19:51, Kaba wrote:
>>> 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:
>>>
>> But the problem is that only a function declaration is needed in
>> order to call a function. How will the caller know what the return
>> type is?
> 
> Perhaps the exact type is not always important to the user, such as in
> this Proto-example. What is important though is the underlying concept
> the type implements. However, without support from the language the
> concept would have to be looked up from the documentation; I could
> live with that:) But something like this would be even nicer:
> 
>      auto[Grammar_Concept] f();
> 

But to the language/compiler, the type is very important. C++ IS a
statically typed language, and all functions have a known (possibly
overloaded) type which included the type of all of the parameters and
the return type.

As far as I know, there is nothing useful that the compiler could do
with a function for which it does not know the return type of. The only
thing that I can think that would even be possible would be to take the
address of it, and cast that to some known function type and stored in a
pointer to function, since the function to be called is uniquely
determined by the parameter signature (we can't overload solely on
return type). This pointer to function could be compared to other
pointer to functions, but could not be dereferenced for a call without a
second cast to the real type of the function, which would require
knowing the return type.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Richard
8/11/2012 10:16:48 PM
Reply:

Similar Artilces:

return type deduction for a function template
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main() { float f1 = 2, f2 = 3; int i1 = 2, i2 = 4; sum<float>(f1, f2); //ERROR float fret = sum<float>(f1, f2); //ERROR } Both the statements marked as "ERROR" throw errors during compilation. Now I can understand the first error in that a return type cannot be possibly deduced. But what I don't understand is the second error. Why is the return type not automatically deduced to be float? Such a deduction happens for arguments of ...

Boost + Python C/API: Mixing python return types with boost return types
Hello, I'm new to Boost & Python and I'm diving straight in by trying to write an extension module to a third party library. Foolishness probably, but I don't have much choice! My question is how can I write a C++ method that will either return a boost wrapped C++ class or a Python object type depending on some internal value? So I want to do something like: PyObject* Variant::getValue() { switch (type) { case INT: return Py_BuildValue("i", union.int); break; case SOMECLASS: ?????? } } But I can...

suffix/deduced return type for functions in C++0x?
Hi, I am not sure what is the status of the "suffix return type" feature in C++0x. Is any of the following codes going to be valid? template<class T, class U> [] mul(T x, U y) { return x*y; } template<class T, class U> auto mul(T x, U y) { return x*y; } template<class T, class U> [] mul(T x, U y) ->decltype(x*y) { return x*y; } template<class T, class U> auto mul(T x, U y) ->decltype(x*y) { return x*y; } Regards, &rzej -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news...

Access to VO Function with Return Type ARRAY from C#
Hi NG! I am trying to Access a VO-DLL-function from C#. This works with Returns in String, Integer or Logical. Trying to Access an ARRAY return (Array of Integer) C# won't marshall the return value. Anybody here who might know how to get this working? Thanks and regards to y'all Kai Hi Kai, > I am trying to Access a VO-DLL-function from C#. This works with > Returns in String, Integer or Logical. > > Trying to Access an ARRAY return (Array of Integer) C# won't marshall > the return value. this cannot work because C# does not know about V...

How to return LhsVar of type 13 or 130 in C interfacing function?
Hello, I am trying to write a C interfacing function which should return a LhsVar of type 13 or 130 (which points to a function, e.g. "sin" or "sinh"). I have studied all interface examples and have had a look at "stack-c.h" and "stack2.c", but I am not familiar with the scilab stack internals, so I have found no solution to this problem. Does anybody know how to do this? I would be very thankful for a comprehensible example. Thank you Dirk On Nov 19, 9:22 am, Dirk Reusch <dirk.reu...@blubber.blah> wrote: > Hello, > ...

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

Q for C++ template Guru: how to use SFINAE to determine meta-function return type?
Hi, I tried but failed to find a solution to a real problem that I experienced. I am hoping that your gurus out there can help me out or point me to the right direction. I striped down the real code to the barebone to demonstrate the problem. I know the posting is still long, so I appreciate your time and efforts. You can find the code at the end of the post. Here is the situation. I have a meta class A that has a member variable whose type is an instance of a meta class B determined by a meta function ( get_B in the code ). Any instance of meta class B logically owns a value whose type is ...

Type of lambda function returning a lambda function...
.... I'm trying to declare a lambda function returning a lambda function, and, unfortunately, I could not use auto key word since it would be the return type of my function. The lambda function looks like this: typedef std::function<void(string)> Func; vector<pair> set; auto lambda = [&set] (string cmd, Func func) { set.push_back(pair(cmd, func)); return lambda; }; return lambda; I try to use std::function template to express that, but.. As you see, an infinite recursion occurs... So how could I do that? Any one have any idea? BTW, the above code compi...

qualified void return type and function return
Condiser this: const void foo(void) { return 0; } const void bar(void) { return; /* removing this line gives the same result */ } freturn() that returns the return type of a function keeps qualifiers on the return type if any, which means that code using its result needs to consider if the qualifiers on it is really necessary. For example, code to compare a function return type to 'void' should treat qualified void types as equivalent to plain 'void.' lcc, however, does not so it says about the code above: > 3: illegal return type; found `int&...

function returning function pointer (recursive type)
Hi group, only today I stumbled over the famous (?) inability of C to define recursive types for functions. The task was to define a daisy chain of functions which facilitate insertion and deletion by passing and returning function pointers of their very own type. Each function should hold a static variable to its' successor. After having unwound my brain from the infinite recursion that happened in the first typedef (*)()(*foo)((*)(...etc. I rummaged the comp.lang.c archives and found the explanation with a workaround based on structs. I remember however that function pointers can be cast...

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...

return type of a function that returns a local variable
Hello, I understand that if a function "f" has a local variable "a", and after it returns, "a" vanishes. If "f" returns "a" as a result, then I noticed the following: 1. if the return type is "a&", then compiler complains reference to the local variable "a". 2. if the return type is "a", then everything works fine. I think this is because in the first case, the return is copy-by- reference, and we can't reference a local var. In the second case, it is a copy-by-value, and it's correct because we can ...

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. Than...

why calendar function return a compiled function type?
As the title. Here is the output from the scilab-4.0-rc1. -1->x=calendar; -1->type(x) ans = 13. -1->x ans = ans(1) Jan 2006 ans(2) M Tu W Th F Sat Sun ans(3) 0. 0. 0. 0. 0. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 0. 0. 0. 0. 0. >From the help file, the return...

Why function TYPE returns "C" not "M" on memo fields in SQLRDD ?
hi, as in subject am. Dear onet: On Oct 18, 3:38 am, "onet" <A MO...@HOTMAIL.COM> wrote: > hi, > as in subject Since the purpose of SQL is (in part) to hide implementation details, do you really need to know that SQL stores data differently than xBase does? The RDD is supposed to smooth out the interface. Hopefully text was put into the field to begin with. Why does it matter to your program? David A. Smith Dear onet: On Oct 18, 3:38 am, "onet" <A MO...@HOTMAIL.COM> wrote: > hi, > as in subject Since the purpose of SQL is (in part) to hi...

incomplete return type in template function declaration; will c++0x 'auto' work?
Suppose I have the following classes: S T U, each of which is incompatible with the others. I want to have functions (ideally written) like this: template<> T foo<0>(const S&); template<> U foo<1>(const S&); Will the template declaration template<int IType> auto foo(const S&); work? Likewise for template<int IType, class Ret> Ret foo(const S&); The problem I see with the second one is that the expression void bar() { S s; U u = foo<1>(s); // * } will be ill-formed. Is such a notation for template foo<int>(c...

C++0x "auto" equivalence in non-0x? (function needs to return undetermined type value)
What I want to do is, template <typename T> void process(const T & value); UndeterminedType generate(); "generate" may return value in various types, which is unknown when it's called. What's determined is that all types are already known and defined. So I know "generate" can return a value in type A, B or C, but I don't know which one it exactly is. The result from "generate" is only used to feed "process", I don't need to store it. But it will be good if I can store it. Seems "auto" keyword in C++0x is introduce...

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...

Is it possible to finding all return values of a C/C++ function?
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate returning value list from the codes. Is it possible? Or is it hard to write one? Thanks. binjobster@gmail.com (in 1155003172.662785.316440@75g2000cwc.googlegroups.com) said: | I'm updating some documents that describes so many C/C++ | functions/method(about 2500). | But I'm not familiar with these codes. So I want to find such a | utility can generate returning value list from the codes. ...

How to pass C++ member function to C function expecting function pointer?
I'm just starting to use C++, and don't want to have to rewrite all of the old C routines that I've been using for 20+ years. One of these is a general purpose function minimizer, which expects to be passed a pointer to the function to be minimized, double func(double p[]) How do I use this to minimize a function defined within a class in C+ +? To be specific, suppose I define the following class, which returns the sum of squared differences between an input vector and a (private) vector called trueParms: class max_func { public: max_func(vector <double> trueParms_) : trueParms(trueParms_) {}; double operator()(double p[]); private: vector <double> trueParms; }; double max_func::operator () (double p[]) { unsigned int i; double temp; double ss; for (i=0, ss=0.0; i< trueParms.size(); i++) { temp = p[i] - trueParms[i]; ss += temp * temp; } return ss; }; In the main code, if I have a vector v containing the values 1.0, 2.0, 3.0, then max_func ff(v); now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2) ^2+ (x[2] - 3)^2 How do I create a function pointer, say ffpointer, in my C++ code that refers to ff, and which I can pass to my C minimizer? Thanks for any suggestions. Richard Stanton RHS wrote: > How do I create a function pointer, say ffpointer, in my C++ code that > refers to ff, and which I can pass to my C minimizer? ...

error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Hi , I was trying to build my project in VC with latest Service Packs but got this error . C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE\atlconv.h(125) : error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I am using some ATL functionality ( String conversion) In Preprocessor setting I have made UNICODE and _UNICODE , but no change in error . Any help is highly solicited . Thanks, Abhijit "Abhijit Bhadra" <abhijit.bhadra@ca.com> wrote in message > Hi , > I was trying to build my project in VC with latest Service Packs but > got this error . > > C:\Program Files\Microsoft Visual > Studio\VC98\ATL\INCLUDE\atlconv.h(125) : error C2440: 'return' : > cannot convert from 'const char *' to 'const unsigned short *' > Types pointed to are unrelated; conversion requires > reinterpret_cast, C-style cast or function-style cast > > I am using some ATL functionality ( String conversion) > In Preprocessor setting I have made UNICODE and _UNICODE , but no > change in error . You have to post some code to get better replies. If it has a lot of non-Standard stuff then try the news:microsoft.vc.* family of newsgroups. Sharad Abhijit Bhadra wrote: > Hi , > I was trying to build my project in VC with latest S...

automatic return-type deduction for lambdas with a single return statement
{ 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 a question about the return-type deduction of lambda expressions (C++11 standard 5.1.2/4). Was there a reason to make the deduction so strict? Would there be any problems to allow automatic deduction when the lambda body contains a SINGLE return statement? I know, once you give someone an inch he will take a mile :) The reason that I would find this very useful is when factoring out certain parts of the expression into local variables - for reasons of repeated sub-expressions or clarity. E.g. <code> auto compute_fa_fb = [](double Na, double Nb, double t) -> std::pair<double, double> { double Fa = std::abs(Na) / t; double Fb = std::abs(std::min(Nb, 0.)) / t; return std::make_pair(Fa, Fb); }; </code> The above lambda isn't a contrived example but part of a formula used in mechanical engineering. I like lambdas as a handy replacement for function-local functions and not every lambda is as short as the above (such that it could be rewritten to spare the two local variables as above), but many of them still contain only a single return statement. Additionally I find it clearer to give sub-results a name to make the lambda-code itself clearer. Any thoughts or objections? --...

New implementation of FSM using functions returning pointers to functions of the same type!
Hi Everyone, Before I go into details I would like to thank Svillen Ranev my professor for solving the "How to define, declare and call a function returning function pointers of the same type?" question I had asked in the context of my compilers class. The problem is this: a function foo foo() taking as parameter a char foo(char) returning a pointer *foo(char) to a function (of the same type) (*foo(char))() taking char (*foo(char))(char) returning a pointer *(*foo(char))(char) to a function (*(*foo(char))(char))() taking char (*(*foo(char))(char))(char) and so on..... This goes on ...

Web resources about - Return-type deduction for all functions - comp.lang.c++.moderated

Return type - Wikipedia, the free encyclopedia
the return type is int . The program can therefore rely on the method returning a value of type int . Various mechanisms are used for the case ...

A Hazard of Covariant Return Types and Bridge Methods
A combination of bridge methods, covariant return types and dynamic dispatch can lead to some surprising and unfortunate results.

Extracting Function Parameter and Return Types in C++
Using the metaprogramming capabilities of C++ and generic programming, it's possible to create an elegant parser of parameter types. Command-line ...

PHP7 - Return Types RFC ongoing vote! : PHP
95 points and 117 comments so far on reddit

The PHP 7 Revolution: Return Types and Removed Artifacts
PHP 7 is well on its way, and some of the changes are upsetting a wide range of people. Do you endorse PHP 7's direction, or are you one to condemn ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Official: Honda CEO confirms return of Civic Type R, announces new Fit-based CUV and super sports bike ...
... Europe have a lot more to look forward to. After a five-year hiatus, Ito confirmed that the Honda Civic Type-R (pictured above) would be returning ...

Which Type of Returns Are You Referring To ?
... measures, how they are employed often is not . I see an awful lot of biases revealed by some quarters always selecting one type of return or ...

Type 2 diabetes, cured by weight loss surgery, returns in one-fifth of patients
A new study shows that although gastric bypass surgery reverses Type 2 diabetes in a large percentage of obese patients, the disease recurs in ...

Could Polaroid’s Beloved Type 55 Film Return—Only Better? One Man Wants to Make It Happen
In the mid-1950s, Ansel Adams was perhaps the most famous photographer alive, and he had a request. For the past several years, he’d been a consultant ...

Resources last updated: 2/22/2016 5:57:51 PM