Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:34:57+00:00 2026-05-26T08:34:57+00:00

I’d like to replace SomeFunction and SetArg with something more generic from boost .

  • 0

I’d like to replace SomeFunction and SetArg with something more generic from boost.
It looks like something that can be done with bind in combination with lambda, but I don’t know how.

This code is very simple but the reason I’d like to replace it is because I need one for 2 and 3 etc arguments.

template<class T>
struct SomeFunction
{
    T value;
    SomeFunction(T s)
        : value(s) {}

    void operator()(T& s)
    {
        s = value;
    }
};

template<class T>
SomeFunction<T> SetArg(T value)
{
    return SomeFunction<T>(value);
}

The requirements:

  • I want a function which returns a function object.
  • When I call this function object, the parameters are passed by reference.
  • The function modifies the objects passed in by reference by setting them to pre-set values.
  • In the code above the pre-set values are passed in by value in the ctor, but any other way is also fine.

The following code demonstrates the usage:

void main()
{
    std::string t;
    SetArg(std::string("hello"))(t);
    assert(t == "hello");
}

Some context:

I want to test the client code of class Foo. So I want to replace the implementation of func1 with my own, but in a flexible way.

struct Foo
{
    virtual void func1(std::string& s)
    {
    }
};

struct MockFoo : public Foo {
    MOCK_METHOD1(func1, void(std::string&));
};

void ExampleTestCase::example()
{
  MockFoo f;
  std::string s;

  EXPECT_CALL(f, func1(_))
      .WillOnce(Invoke(SetArg(std::string("hello"))));

  f.func1(s);

  CPPUNIT_ASSERT_EQUAL(std::string("hello"), s);
}

Invoke takes a function or function object. Inside the new implementation of func1 it calls the function object returned by SetArg and sets its argument to the string "hello".

Invoke is part of gmock/gtest but SetArg is not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:34:58+00:00Added an answer on May 26, 2026 at 8:34 am

    Here is what I came up with. The operator() of setter would probably
    require some tweaking as we are not really benefiting from the
    possible move semantics here, but I can’t figure that out right now.

    Also note that this makes heavy use of C++11 features which might not
    be available to you.

    #include <string>
    #include <iostream>
    #include <tuple>
    
    // set arbitrary values to 
    template<typename... Args>
    struct setter {
      // needed because we cannot use initializer lists as they require assignment
      setter(const std::tuple<Args&...>& t) : t(t) {}
      std::tuple<Args...> t;
    
      // again a template to trigger deduction again
      template<typename... Args2>
      void operator()(Args2&&... args) {
        t = std::make_tuple(args...);
      }
    };
    
    template<typename... Args>
    setter<Args&...> create_setter(Args&... args) {
      return setter<Args&...>(std::tie(args...));
    }
    
    int main()
    {
      int i = 0;
      long l = 1;
      std::string foo = "foo";
    
      auto s = create_setter(i, l, foo);
    
      s(23, 42, "bar");
      std::cout << i << std::endl;
      std::cout << l << std::endl;
      std::cout << foo << std::endl;
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.