Solved

boost::add_reference - template not matched

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 120
is twain_32.dll cmpatible with windows 10 ? 10 237
egit plugin on eclipse 8 100
Visual Studio 2013 and Eclipse Neon 6 72
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

733 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