Solved

boost::add_reference - template not matched

Posted on 2006-06-12
2
436 Views
Last Modified: 2013-12-14
Here's the code:

#include <string>
//#include <boost/type_traits/add_reference.hpp>
using namespace std;

template<class S>
struct add_reference {
    typedef S & type;
};

template <class S>
//void test(S & x) // does work
//void test(typename boost::add_reference<S>::type x) // not work
void test(typename add_reference<S>::type x) // not work
{ }

int main() {
    string x("asdf");
    test(x);
    //test<string>(x); // always works
    return 0;
}

The above code gives the following error under G++ and MSVC++ respectively:

   error: no matching function for call to `test(std::string&)'
   error C2783: 'void test(add_reference<S>::type)' : could not deduce template argument for 'S'

Why is the type not able to be deduced using the "add_reference" trick?
0
Comment
Question by:ext2
  • 2
2 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 16886513
Compilers can often deduce types for template functions, but not template classes.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 100 total points
ID: 16886713
Having said that, that's not what's going wrong for you. It is simply a function template that the compiler is needing help with. I guess typename add_reference<S>::type makes it too complicated for the compiler to figure out what S is in the expansion of the function template. Once the compiler has successfully figured out the type of S, you can use typename add_reference<S>::type to get a reference to S.

e.g.

template <class S>
void test(S & x)
{
      typedef typename add_reference<S>::type x_ref;
      x_ref x2 = x;
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question