Solved

boost::add_reference - template not matched

Posted on 2006-06-12
2
432 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

18 Experts available now in Live!

Get 1:1 Help Now