1

Following C++ polymorphism with boost scoped_ptr, I would like to understand the rules of C++ argument deduction. For example:

  1. the rules for C++ polymorphic substitutions, when smart pointers and containers are used
  2. automatic conversions, between const and non-const arguments
  3. when can temporaries be passed as references?
  4. how do the rules differ in template argument deduction.

If there is a relevant section in the standard you can point to that would help too.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ATemp
  • 319
  • 3
  • 10
  • 1
    Very good question, but it's very broad. Might want to break it down into multiple questions. – Luchian Grigore Mar 24 '12 at 19:24
  • OK. Is it better to have this as a community wiki? I will refactor. – ATemp Mar 24 '12 at 19:27
  • What exactly are you unsure about? At which point was your textbook unclear? – Kerrek SB Mar 24 '12 at 19:27
  • In the question, http://stackoverflow.com/questions/9845584/c-polymorphism-with-boost-scoped-ptr, the polymorphic substitution for const reference but not for non-const reference surprised me. – ATemp Mar 24 '12 at 19:29
  • 1
    @ATemp: That question doesn't really have anything to do with polymorphism, but rather with temporaries binding to const-references. As with most of your question, you're touching on a million little parts of C++ which are probably best mastered by learning the language more rather than asking for a be-all-end-all recipe. – Kerrek SB Mar 24 '12 at 19:42

1 Answers1

0

I think this question is far too broad and I don't understand what all the questions are. Jere is my shot:

  1. Template deduction doesn't care about base classes or conversions. It deduces a static type. Only if there is no direct match it will try if a conversion can solve this and as usual it will only apply one conversion. For example it the type passed is a raw pointer and the expected type is a smart pointer which can be implicitly converted from a build-in pointer it should find this.
  2. For value types constness is ignored. For reference or pointer types a non-const pointer or reference can convert to the corresponding non-const version. The deduced tupe won't have cv-qualifiers, however, although it can be possible to instantiate the same function explicitly with cv-qualifird types. It you deduce abT&& different rules apply.
  3. Temporary can only bind to const qualified lvalue references and to rvalue references. This is universal in the language.
  4. What is this comparison against?
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380