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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:49:52+00:00 2026-05-21T05:49:52+00:00

Suppose I have a class template <typename T> class A { public: template <typename

  • 0

Suppose I have a class

template <typename T>
class A {
 public:
  template <typename V>
    void f(std::tr1::shared_ptr<const std::vector<V> > v1, 
           std::tr1::shared_ptr<const std::vector<float> > v2) {}
};

The following does not compile:

 A<string> a;
  std::tr1::shared_ptr<std::vector<float> > v1(new std::vector<float>());
  std::tr1::shared_ptr<std::vector<float> > v2(new std::vector<float>());
  a.f(v1, v2);

The compiler error is:

error: no matching function for call to 'A<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::f(std::tr1::shared_ptr<std::vector<float, std::allocator<float> > >&, std::tr1::shared_ptr<std::vector<float, std::allocator<float> > >&)'

The compiler could not make std::tr1::shared_ptr<std::vector<float> > into
std::tr1::shared_ptr<const std::vector<float> > for the first argument. Yet it could for the second (non-template argument).

One solution to this is to change the call to f(), call it like this f<float>(...).
Another solution is to declare v1 as shared_ptr to const vector<float>.

  1. Why is template instantiation behaving so differently here?
  2. My understanding of having a shared_ptr<const T> as argument to a method is that the method cannot change what the shared_ptr is pointing to. If we change the shared_ptrs to raw pointers and v1, v2 as raw pointers to vectors, then the code will compile fine. What is it about shared_ptrs that breaks template deduction?
  • 1 1 Answer
  • 3 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-21T05:49:53+00:00Added an answer on May 21, 2026 at 5:49 am

    Why is template instantiation behaving so differently here?

    A shared_ptr<T> is implicitly convertible to a shared_ptr<const T> via a converting constructor. Unfortunately, such conversions cannot be used during template argument deduction.

    In order for argument deduction to succeed, the type of the argument has to match the type of the parameter exactly; most conversions, including user-defined conversions, are not used.

    Only a few very basic conversions are allowed, including array-to-pointer decay and function-to-function-pointer conversion (since no function parameter can be of array type or function type, these conversions cannot lead to confusion). In addition, top-level const- and volatile- qualifiers are wholly ignored.

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

Sidebar

Related Questions

suppose I have a file alpha.h: class Alpha { public: template<typename T> void foo();
Suppose we have a class: template <class Type> class A { public: void function1(float
Suppose I have: template<typename T> class A { typedef T t_type; void done_work(); };
Suppose a construct like this: class Interface { public: template <typename T> virtual void
Suppose we have template class template <typename T> class MyTem{ public: bool is_T_Pointer(){ <...>
gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template<typename realT> class B
Suppose we have the following template class template<typename T> class Wrap { /* ...
Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have the following: #include <iostream> #include <string> template<class T> class base {

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.