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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:24:51+00:00 2026-06-08T03:24:51+00:00

I have a struct like this: struct VrtxPros{ long idx; std::vector<std::string> pros; VrtxPros(const long&

  • 0

I have a struct like this:

struct VrtxPros{
    long idx;
    std::vector<std::string> pros;
    VrtxPros(const long& _idx=-1, const std::string& val="") : idx(_idx)
    {
         if ( !val.empty() && val!="" )
             pros.push_back(val);
    }
};

and later in the code I use it like that:

long idx = 1234;
VrtxPros vp( 2134, std::string("-1") );
if ( margin ) vp.pros[0] = idx;

The compiler has no problem with that. I am wondering because the operator should deliver a reference.
I could not find an operator= in std::string which would accept a long as source.

Why does the code compile?

  • 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-08T03:24:55+00:00Added an answer on June 8, 2026 at 3:24 am

    A std::string can be assigned to a char, and a long can be implicitly converted to a char, so a std::string can be assigned to a long. Your compiler will probably give a warning about this kind of implicit conversion (turn up the warning level and you’ll see it, if you don’t already).

    See the #4 operator= listed here. Notice no constructor overload takes just a char, so this sort of thing can only be done for assignment.

    For that matter, you can do this too:

    std::string wow;
    wow = 7ull; // implicit unsigned long long to char conversion
    wow = 1.3f; // implicit float to char conversion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
I have a struct like this: typedef struct string { unsigned long length; unsigned
Hi I have a struct like this typedef struct { string firstname; string lastname;
I have a struct like this: public struct MyStruct { public string Name; public
I have a struct like this: public struct Vehicles { public string Name {
hello i have an struct like this: struct Student { string name,lettergrade; int stdnumber,*examgrades;
I have defined my struct like this: struct Test { private string assayName; public
Ok so I have struct like this typedef struct { float x; float y;
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
If I have a struct like this: typedef struct { unsigned char c1; unsigned

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.