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

  • Home
  • SEARCH
  • 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 4619820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:28:34+00:00 2026-05-22T02:28:34+00:00

I have a class with an implicit const char * constructor. My intuition tells

  • 0

I have a class with an implicit const char * constructor. My intuition tells me that it shouldn’t matter whether I implicitly or explicitly invoke the constructor, but apparently it does matter in the case of const char * and I don’t understand why. The VC++ compiler matches foo(const String &t) for the first call to foo in the code below:

struct Str
{
    Str(const char *c)
    {
        value = c[0];
    }
    Str(double c)
    {
        value = char(c);
    }
    char value;
};

void foo(const Str &t)
{
    cout << "const Str &t matched\n";
}

void foo(const Str &&t)
{
    cout << "const Str &&t matched\n";
}

void main()
{
    foo("v");
    foo(Str("v"));
    foo(5.0);
    foo(Str(5.0));
}

It matches the Str &&t version of foo for everything else. Why does it behave differently in the implicit “v” case? And what should I change to get the && version of foo to match? (imagine foo is for example a vector::push_back function — I would rather not have to explicitly cast all my string literals.)

  • 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-22T02:28:35+00:00Added an answer on May 22, 2026 at 2:28 am

    It’s a known issue with the specification of rvalue references, which is now fixed but wasn’t discovered when MSVC10 implemented theirs. Basically, when deciding whether to bind to lvalue or rvalue references, the original lvalueness or rvalueness of the argument is considered, not after any conversions might take place if they are required. This is because the original rvalue references were too greedy about binding to lvalues and some strong wording was put in place to ensure the safety of rvalue references, but it turned out to be a little over-zealous. You don’t need a custom type to demonstrate this- a simple std::string will do fine.

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

Sidebar

Related Questions

I have a class with a templated constructor for implicit move conversion, however this
I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(
I have a class similar to the following that uses an internal List: public
I have a templated class holding a value. Is it possible to add a
I have a problem. I'm trying to create an array of different class objects
I have this very simple wrapper template: template<class T> struct wrapper { inline operator
I have this class hierachy: class A { private String a1; private Class B
I want to provide a templated function that converts most basic types to string.
My apologies if this is a dupe. I found a number of posts re.
The last question I asked was something I stumbled upon when trying to understanding

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.