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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:01:37+00:00 2026-06-12T10:01:37+00:00

I tried to introduce some const correctness (actually functional paradigms) to some new code

  • 0

I tried to introduce some const correctness (actually functional paradigms) to some new code and found that I cannot pass an std::shared_ptr<A> to a function that expects an std::shared_ptr<A const>. Note that I don’t want to cast away constness but introduce it, which is legal with raw pointers.

Is there some way to get around this? I didn’t find a member-function to do this.


The precise error uttered by g++ 4.6.1 is:

error: no matching function for call to ‘foo(std::shared_ptr<A>)’
note: candidate is:
note: template<class T> std::shared_ptr<_Tp> foo(std::shared_ptr<const _Tp>)
  • 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-12T10:01:39+00:00Added an answer on June 12, 2026 at 10:01 am

    The problem in your case is not with the possible conversions from/to different std::shared_ptr, but is more related to how type inference works for template functions.

    When the compiler tries to match a function call with a template, it will only accept exact matches, i.e. no type conversions at all. In this case, your function takes a std::shared_ptr<const T>, and the caller has a std::shared_ptr<U> where U is not const. Because the match is not exact, it will discard the template and pick the next overload candidate.

    The simple workarounds are: avoid type inference altogether and provide the template argument:

    std::shared_ptr<A> p;
    foo<A>(p);             // will use the templated shared_ptr conversion
    

    Or perform the conversion yourself:

    foo(std::shared_ptr<const A>(p));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New Ant 1.8.0 (release Feb 1) introduces some cool features, so I tried my
I've got some code here for printing that prints a block of information from
Tried searching the site, but cannot find an answer to my problem: Lets say
EDIT I tried debugging this with xdebug and netbeans. It's weird that the exports
I am trying to convert some code from using DOM (via jDOM) to use
I am having some issues with an extension that i am trying to make.
I'm reading the Rails 3 In Action book and they introduce some BDD with
I'm working on a simple video device and I'd like to introduce some standard
I am trying to test some classes that rely on a Task to do
I am trying to introduce a staging step in my company's code-production process. We

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.