Solved

boost::add_reference - template not matched

Posted on 2006-06-12
2
428 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
Comment Utility
Compilers can often deduce types for template functions, but not template classes.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 100 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now