Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

boost::add_reference - template not matched

Posted on 2006-06-12
2
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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