This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: A link error related variadic-template
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: chmodexplorer at sina dot com
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 30 Mar 2012 10:51:59 +0100
- Subject: Re: A link error related variadic-template
- References: <20120330063833.650B3D24B66@webmail.sinamail.sina.com.cn>
On Mar 30, 2012 7:38 AM, <chmodexplorer@sina.com> wrote:
>
> I used g++ 4.7.0 to compile the piece of code:
>
> #include <stdio.h>
> #include <boost/call_traits.hpp>
>
> using namespace boost;
>
> template <typename... Args> long add(Args...);
This function is not defined.
> template <typename T> long add( typename call_traits<T>::param_type t) { return t; }
This function overloads args() but it can never deduce the parameter
T, because T appears in a non-deduced context (to the left of the ::
scope operator)
> template <typename A, typename... Args>
> long add( typename call_traits<A>::param_type a, typename call_traits<Args>::param_type... args )
Same here, this adds a third overload of args (it does not provide a
definition for the first overload above) and because they appear in
non-deduced contexts A and Args cannot be deduced.
> {
> ? ? ? ?return a + add( args... );
> }
>
> int main()
> {
> ? ? ? ?char c = 0;
> ? ? ? ?short s = 1;
> ? ? ? ?int i = 2;
> ? ? ? ?long l = 3;
> ? ? ? ?long n = add( c, s, i, l );
This cannot call the third overload, because argument deduction cannot
succeed, so it calls the first overload of add() which you haven't
defined so you get a linker error.
>
> ? ? ? ?return 0;
> }
>
> And I encountered a link error:
> /tmp/cc8rJ4xe.o: In function `main':
> variadic-templates.cpp:(.text+0x33): undefined reference to `long add<char, short, int, long>(char, short, int, long)'
> collect2: error: ld returned 1 exit status
>
> Is there anyone who can help me to resolve the problem? Thanks very much!