Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockFunction improvements #2319

Closed
wants to merge 5 commits into from
Closed

Conversation

adambadura
Copy link
Contributor

No description provided.

@adambadura
Copy link
Contributor Author

adambadura commented Jul 11, 2019

It seems the change 3d70a6b (Add MockFunction::AsLambda (#2278)) will not build since it contains:

template <typename R, typename... Args>
class MockFunction<R(Args...)> {
  /* ... */

  auto AsLambda() {
    return [this](Args... args) -> R {
      return this->Call(std::forward<Args>(args)...);
    };
  }

  /* ... */
};

This form of auto return type deduction requires C++14. While apparently here we have only C++11.

If this cannot be changed (seems reasonable to not require too much) then the code either would have to use preprocessor for providing AsLambda with C++14 and higher only or would have to be dropped altogether. Which is fine as well, from my point of view the key parts are bb1c776 (Document MockFunction::AsStdFunction (#2265)) and cd3b9df (Add support for std::function in MockFunction (#2277)) anyway.

I was using CMake while developing. (I tried the Bazel but didn't understand it at all...) This means that probably CMake is under-configured by not being strict on the language standard as use. Otherwise, I would see the error immediately. If C++11 is indeed the required standard I will try to update CMake correspondingly.

@adambadura
Copy link
Contributor Author

I was using CMake while developing. (I tried the Bazel but didn't understand it at all...) This means that probably CMake is under-configured by not being strict on the language standard as use. Otherwise, I would see the error immediately. If C++11 is indeed the required standard I will try to update CMake correspondingly.

This seems more complicated. The main CMakeLists.txt does include

if (CMAKE_VERSION VERSION_LESS "3.1")
  add_definitions(-std=c++11)
else()
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  if(NOT CYGWIN)
    set(CMAKE_CXX_EXTENSIONS OFF)
  endif()
endif()

It seems to be an issue between CMake and Visual C++, even though I have recent versions of both (3.14.0 and 19.21.27702.2 respectively) and this Visual C++ does support specifying language standard. Maybe the issue is that it does not support specifying C++11, only C++14 or higher.

The C++11 config does work with Cygwin.

gmock-spec-builders.h uses std::function (in MockFunction) but did
not include <functional> to provide it. Apparently, it worked since
the header must have been included by something else but better be
safe than sorry.
Use of the = default instead of {} provides some advantages like
proper constexpr or noexcept support. Furthermore, it keeps the type
aggregate and trivial if it otherwise would be.

MockFunction doesn’t truly benefit from those. However, it doesn’t
also lose anything on using = default instead of {}. While it does
add uniformity, follows guidelines (for example Chromium style
guide), and protects against eager static checkers.
@adambadura
Copy link
Contributor Author

adambadura commented Jul 11, 2019

I have dropped the problematic change 3d70a6b (Add MockFunction::AsLambda (#2278)). Instead, I have added a change updating CMake support under Cygwin.

Copy link
Contributor

@kuzkry kuzkry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. recently there's been some rename of GoogleMock docs but your change should easily rebase onto it.


std::function<R(Args...)> AsStdFunction() {
return [this](Args... args) -> R {
return this->Call(std::forward<Args>(args)...);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other std::forwards won't forward anything. args can never be variadic universal references here as they are not function template parameters. Imo, std::move works better here (as it will try to move, unless not possible (l-refs and nonmovable types)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, note that I haven't written it so. I only moved it a bit. It was so before.

Secondly, I think you are not right about it anyway. This was originally std::move back in time when the class was still located in gmock-generated-function-mockers.h but was corrected to std::forward by 7796273 related to #1867.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry then, thanks for a good reference to another PR.

@gennadiycivil
Copy link
Contributor

Thank your for this contribution. Could you please elaborate and provide more information on exactly the issue / problems this is trying to address.
What is the before/after effect this would create and how is this beneficial?

@adambadura
Copy link
Contributor Author

adambadura commented Jul 15, 2019

Btw. recently there's been some rename of GoogleMock docs but your change should easily rebase onto it.

Well, it was rebased over the HEAD until your changes got merged in. ;)

@adambadura
Copy link
Contributor Author

Thank your for this contribution. Could you please elaborate and provide more information on exactly the issue / problems this is trying to address.
What is the before/after effect this would create and how is this beneficial?

Essentially there are three changes here:

  1. Minor correction. (1st and 2nd commits)
  2. Document currently undocumented MockFunction::AsStdFunction. (3rd commit)
  3. Allow MockFunction to accept std::function (or other similar by user extension) as template argument and extract the function signature out of it. (4th commit)
  4. Minor improvement. (5th commit)

Copy link
Contributor

@kuzkry kuzkry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. recently there's been some rename of GoogleMock docs but your change should easily rebase onto it.

Well, it was rebased over the HEAD until your changes got merged in. ;)

It's only a kind conflict reminder of mine ;)


std::function<R(Args...)> AsStdFunction() {
return [this](Args... args) -> R {
return this->Call(std::forward<Args>(args)...);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry then, thanks for a good reference to another PR.

@gennadiycivil
Copy link
Contributor

@adambadura Thank you for this request. There are too many things going on at the same time here.
I see documentation changes, CMake changes, code changes, etc.
I recommend splitting this into separate small PRs so we can take a proper look.
Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants