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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:23:34+00:00 2026-05-30T22:23:34+00:00

Why does this code (unassigned temporary variable constructed from a const char* variable): class

  • 0

Why does this code (unassigned temporary variable constructed from a const char* variable):

class A
{
public:
    A(const char*) {}
};

int main()
{
    const char* constCharPointerVariable = "StringLiteral";
    A(constCharPointerVariable);
    return 0;
}

Give these errors?

error C2512: 'A' : no appropriate default constructor available
error C2040: 'constCharPointerVariable' : 'A' differs in levels of indirection from 'const char *'

Whereas this code (assigned temporary variable constructed from a const char* variable):

class A
{
public:
    A(const char*) {}
};

int main()
{
    const char* constCharPointerVariable = "StringLiteral";
    A a(constCharPointerVariable);
    return 0;
}

Gives no errors.

And this code (unassigned temporary variable constructed from a const char* variable static_cast to a const char*):

class A
{
public:
    A(const char*) {}
};

int main()
{
    const char* constCharPointerVariable = "StringLiteral";
    A(static_cast<const char*>(constCharPointerVariable));
    return 0;
}

Gives no errors.

Bonus points if you can provide the section number in the C++ specification that specifies the first code sample to be not allowed.

  • 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-30T22:23:35+00:00Added an answer on May 30, 2026 at 10:23 pm
    A(constCharPointerVariable);
    

    This is actually a declaration of a variable of type A named constCharPointerVariable. It does not create a temporary object.

    If you used clang, you’d get the more helpful error message:

    error: redefinition of 'constCharPointerVariable' with a different type
        A(constCharPointerVariable);
          ^
    

    As a simpler example, the following is invalid because it declares two int objects in the same scope, both named x:

    int x(0);
    int (x);
    

    As for why the code is parsed this way, you can find the syntax rules for Declarators in
    §A.7 of C++11. Basically, when you declare a variable, you can enclose its name in any number of parentheses.

    Relevant productions include:

    • declarator -> ptr-declarator
    • ptr-declarator -> noptr-declarator | declarator-id
    • noptr-declarator -> ( ptr-declarator )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does this code cause a memory leak: int main(){ int * a = new
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Why does this code not print an exception stack trace? public class Playground {
Why does this code stop with segmentation fault : class MapFile { public: /*
Ok, consider the following code: private const int THRESHHOLD = 2; static void Main(string[]
Why does this code always produce x=2 ? unsigned int x = 0; x++
See this code snippet int main() { unsigned int a = 1000; int b
Please see my code: #include <stdint.h> int main(int argc, char *argv[]) { unsigned char
I'll start with the code: typedef std::vector<unsigned char> CharBuf; static const int RCV_BUF_SIZE =
Does this code work across all standard compliant C++ compilers (it works with g++)?

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.