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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:21:15+00:00 2026-06-18T00:21:15+00:00

Compiling with clang++ 4.1: class A { public: A(const char *s=0) : _s(s) {}

  • 0

Compiling with clang++ 4.1:

class A
{
public:
    A(const char *s=0) : _s(s) {}
    const char *_s;
};

void f(A a)
{
    cout << a._s << endl;
}

int main()
{
    f("test");
    return 0;
}

prints,

test

whereas if I define f as follows,

void fA(A &a)
{
    cout << a._s << endl;
}

I get a compilation error,

clang++ -std=c++11 -stdlib=libc++ -o test test.cpp
test.cpp:14:5: error: no matching function for call to 'f'
    f("9000");
    ^
test.cpp:7:6: note: candidate function not viable: no known conversion from
      'const char [5]' to 'A &' for 1st argument;
void f(A &a)
     ^

Why? I don’t understand why making f take a reference leads to problems.

  • 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-18T00:21:16+00:00Added an answer on June 18, 2026 at 12:21 am

    f("test"); attempts to create a temporary A – f(A("test"));, but that temporary can’t bind to a non-const reference. The pass-by-value is okay because a copy is theoretically made (in practice, copy elision will occur).

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

Sidebar

Related Questions

When compiling : #include <vector> template<class T> class foo { void bar() { std::vector<T>
Compiling the following: // file main.cpp #include <string> #include <boost/lexical_cast.hpp> int main() { boost::lexical_cast<std::string>(
I have a function defined as: int f_2() { rand(); return 10; } clang
Yesterday I installed clang 3.1 and g++ 4.7 and tried compiling a project I'm
When compiling a c file that uses old style function definition like int foo(a)
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
While compiling some C++11 code with both GCC 4.7.2 and Clang 3.1, I ran
When compiling C source code with either gcc or Clang, I always use the
When compiling my project with clang++, the path to the source files is apparently
I've decided to try out Clang by compiling a c++ project of mine that

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.