Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 auto keyword in C++14 lambda not auto-completing
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

HateDread
Junior Member

Australia
23 Posts

Posted - Jan 26 2017 :  09:19:59 AM  Show Profile  Reply with Quote
Currently using latest VAX with VS2017 RC 26020.00 (January 5, 2017).

The use of auto in the following lambda leads to no auto-complete info or type info on hover-over of the 'param' variable. This occurs with and without Intellisense database enabled, as well as Enhanced Listboxes set to sourcing from either VAX or Intellisense.

#include <vector>
#include <functional>
#include <iostream>
#include <string>

struct SomeClass
{
	int m_num;
};

std::function<void(SomeClass)> g_callback;

void CallbackBindTest(std::function<void(SomeClass)> a_callback)
{
	g_callback = a_callback;
}

int main()
{
	// no info about 'param' given here -
	// no auto-completed members, no type on hover.
	CallbackBindTest([](auto param) {
		std::cout << param.m_num << std::endl;
	});

	// invoke callback just to test (it will print '5')
	g_callback(SomeClass{ 5 });

	std::cin.get();
	return 0;
}


Thanks!

feline
Whole Tomato Software

United Kingdom
18749 Posts

Posted - Jan 26 2017 :  7:37:45 PM  Show Profile  Reply with Quote
This code compiles, but I am confused by how it works. I can follow that the template function takes the "SomeClass" type, but the lambda function does not seem to have any restriction on the types it can be called with. So what stops me calling the lambda with a different class type?

I am not following what limits / defines the type of the auto. I am seeing the same problem, I am just trying to work out if this is something that can be reasonably determined without compiling the code.

zen is the art of being at one with the two'ness
Go to Top of Page

HateDread
Junior Member

Australia
23 Posts

Posted - Jan 26 2017 :  8:36:40 PM  Show Profile  Reply with Quote
quote:
Originally posted by feline

This code compiles, but I am confused by how it works. I can follow that the template function takes the "SomeClass" type, but the lambda function does not seem to have any restriction on the types it can be called with. So what stops me calling the lambda with a different class type?

I am not following what limits / defines the type of the auto. I am seeing the same problem, I am just trying to work out if this is something that can be reasonably determined without compiling the code.



If you try to invoke g_callback with something other than a SomeClass object, it won't compile, i.e.

// invoke callback just to test (it will print '5')
	g_callback(SomeClass{ 5 });

	// fails - "cannot convert argument 1 from 'int' to 'SomeClass'"
	g_callback(5);


It appears that the std::function<void(SomeClass)> argument allows the compiler to deduce the auto type in the lambda (it can cast a lambda to an std::function, after all).

EDIT: Well, not being able to invoke it with other types is just default behaviour of std::function. Now I'm not sure if you could determine the auto type.

Edited by - HateDread on Jan 26 2017 9:07:03 PM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18749 Posts

Posted - Jan 27 2017 :  08:22:38 AM  Show Profile  Reply with Quote
I have done very little with lambda's so far, so I may well be missing something here, so feel free to correct me if I am wrong on anything here

The fact that the IDE it's self is not able to work out the auto type is what got me wondering. In VS2010 and above VA defaults to the IDE intellisense parser, since the parser has gotten better over the years.

Going back to the code, and trying to follow it again, I had a look online, and found this post, which helped me a bit with my understanding of std::function<>

http://stackoverflow.com/questions/14677997/stdfunction-vs-template#14678298

I was thinking of this code in terms of inheritance and virtual functions, which seems reasonable. Here, if I follow correctly, the std::function can be called with SomeClass or anything derived as public from SomeClass. So you are limited to only the member list SomeClass provides, but the full member list of the passed type / function at run time may be bigger.

But why "void" in std::function<void(someClass)> ? This keeps confusing me.

If I am following this code correctly, it makes sense that "auto" in the lambda must be SomeClass, or something derived from SomeClass, as if this was a normal class pointer.

Does this make sense, or am I going in circles here?

zen is the art of being at one with the two'ness
Go to Top of Page

HateDread
Junior Member

Australia
23 Posts

Posted - Jan 27 2017 :  08:31:25 AM  Show Profile  Reply with Quote
I'll set aside the design issues with what I have (since that link does argue for templates, and I could probably do things a better way than this, but that's another day :) ).

The 'void' in that template is the return type, e.g. std::function<int(int, int)> could be bound with the following lambda - [](int a, int b) { return a + b; }.

I'm not super clear on the generic lambda internals, but I believe it just makes a templated operator() method - http://stackoverflow.com/questions/17233547/how-does-generic-lambda-work-in-c14

Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18749 Posts

Posted - Jan 28 2017 :  11:21:29 AM  Show Profile  Reply with Quote
I only posted the link since it was something I was using to try and help me work out and follow this code. I think I now see and understand what is happening, you are setting the function pointer (the std::function) to the lambda expression, so the lambda is being cast to the function pointer type, which tells us the type the parameter must be. So the auto can be worked out at compile time, since it is a cast. Just not one that I was familiar with

I have put in a bug report for this:

case=103392

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000