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 8591051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:26:14+00:00 2026-06-11T23:26:14+00:00

This regrettably lengthy example is a cut-down version of a real piece of code

  • 0

This regrettably lengthy example is a cut-down version of a real piece of code I am attempting to write. It has two problems. First, as written, it doesn’t compile: the error is

error: cannot bind ‘std::string’ lvalue to ‘std::string&&’
error:   initializing argument 3 of ‘void pack_arg(datum*, size_t, std::string&&)’

and is triggered by the presence of the call to shut_the_door. If I take out the && in the relevant overload of pack_arg, then it compiles, but the generated code appears to copy both strings, unnecessarily. Second, I need to ensure that all string temporaries that the array d is holding pointers to, remain live until after the call to vprocess. Right now the generated code appears to destruct them right before that call.

I am willing to consider quite radical changes to how the array is filled, as long as I have the opportunity to run each argument to process through an overloadable function call, independent of all the other such arguments. (In particular, I suspect that the special rules for temporaries initializing a reference would make problem 2 go away if I could use std::string& in the union, but as each member of the datum array is filled in by assignment rather than initialization, that presently won’t work, and I don’t know how to make it be filled in by initialization without losing the calls to pack_arg, which are necessary in the larger context.)

EDIT: Using std::forward between pack_args and pack_arg does not appear to help; I get exactly the same error messages.

#include <string>
#include <stddef.h>
using std::string;

union datum
{
  const string* s;
  int i;
};

inline void
pack_arg(datum* d, size_t n, int t)
{
  d[n].i = t;
}

inline void
pack_arg(datum* d, size_t n, string && t)
{
  d[n].s = &t;
}

inline void
pack_args(datum*, size_t)
{
}

template <typename X, typename... XS> inline void
pack_args(datum* d, ::size_t n, X&& x, XS&&... xs)
{
  pack_arg(d, n, x);
  pack_args(d, n+1, xs...);
}

extern void vprocess(datum*, size_t);

template <typename... XS> inline void
process(XS&&... xs)
{
  size_t n = sizeof...(xs);
  datum d[n];
  pack_args(d, 0, xs...);
  vprocess(d, n);
}

extern string shut_the_door();

void
demo()
{
  process(1, 2, "buckle my shoe", 3, 4, shut_the_door());
}
  • 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-06-11T23:26:15+00:00Added an answer on June 11, 2026 at 11:26 pm

    Luc brings a really interesting point (I briefly skimmed the code reading only the signatures):

    If you are going to store pointers to the std::string in a container then your code must make sure that the arguments are not rvalues, as that will leave a dangling pointer after the function call completes.

    The more I think on your code, the more I believe that the whole thing is just a bad idea. Without a tag on what each element is, you cannot know after the fact which element of the array is of what type. The lifetime of the objects will be hard to maintain (for the std::strings in particular…) Maybe you should rethink the design altogether.


    You have hit an interesting quirk of the new C++11 syntax. The double ampersand (&&) in these two declarations don’t really mean the same:

    inline void
    pack_arg(datum* d, size_t n, string && t);         // [1]
    template <typename X, typename... XS> inline void
    pack_args(datum* d, ::size_t n, X&& x, XS&&... xs) // [2]
    

    In [1] the argument of type string&& will only bind to an rvalue allowing to discriminate a temporary (or std::moved object) from an lvalue. The rvalue-reference behaves like an lvalue inside the function. In [2], because you are in a template and X is an inferred type a different set of rules apply and X&& will be resolved to either an lvalue reference or rvalue reference, depending on the argument.

    In your case, the argument is actually a temporary, so X&& will be mapped to std::string&&, but inside the function it is treated as an lvalue an cannot be bound. You can fix this by adding std::forward:

    template <typename X, typename... XS> inline void
    pack_args(datum* d, ::size_t n, X&& x, XS&&... xs)
    {
      pack_arg(d, n, std::forward(x));
      pack_args(d, n+1, std::forward(xs)...);
    }
    

    The std::forward template will generate either an lvalue or rvalue reference depending on whether the argument is an lvalue or rvalue-reference itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an example of the code am using. I use a modified version
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
This code won't compile because there is an illegal reference to a static field.
This is my code: $.ajax({ url: someUrl, type: PUT, data: { id: id, message:
This has been asked several times, i know, but help me understand something. You
This same installation of Weka has loaded for me in the past. I am
This has been bugging me lately. Say I have a base class Base. If
This must be a very simple solution that has eluded me this last hour.

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.