ISO C++ compile error

Z

Zach

Using Dev-C++ 4.9.8.5 I get error:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\winroot\src\c++\stupid_tricks\functor_trick.cpp" -o
"C:\winroot\src\c++\stupid_tricks\functor_trick.exe"
-I"C:\DEV-CPP\include\c++" -I"C:\DEV-CPP\include\c++\mingw32"
-I"C:\DEV-CPP\include\c++\backward" -I"C:\DEV-CPP\include"
-L"C:\DEV-CPP\lib"
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp: In member function
`get_result_type<Base, T, U>::result_type X<Base,
void>::eek:perator()(const
T&, const U&)':
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:47: warning:
`typename
get_template_parameter<Base, T, U>::result_type' is implicitly a
typename

C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:47: warning:
implicit
typename is deprecated, please see the documentation for details

C:/winroot/src/c++/stupid_tricks/functor_trick.cpp: In function `int
main(int,
char**)':
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:59: `multiplies'
undeclared

(first use this function)
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:59: (Each
undeclared
identifier is reported only once for each function it appears in.)
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:59: template
argument 1 is
invalid
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:59: ISO C++ forbids
declaration of `x' with no type
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:60: template
argument 1 is
invalid
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:60: ISO C++ forbids
declaration of `y' with no type
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:61: `plus'
undeclared (first
use this function)
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:61: template
argument 1 is
invalid
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:61: ISO C++ forbids
declaration of `z' with no type
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:62: template
argument 1 is
invalid
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:62: ISO C++ forbids
declaration of `t' with no type
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:65: `x' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:66: `y' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:67: `y' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:68: `z' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:69: `t' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:70: `t' cannot be
used as a
function

C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:73: `t' cannot be
used as a
function
C:/winroot/src/c++/stupid_tricks/functor_trick.cpp:74: `t' cannot be
used as a
function

Execution terminated


Here is the code:

#include <functional>
#include <iostream>

// handle mixed type operations by selecting a "winning" type to
promote to
// By default this should be symmetric.
template <class T, class U> struct promote : public promote<U,T> { };
template <class T> struct promote<T,T> { typedef T result_type; };
template <> struct promote<double,int>{ typedef double result_type; };


// get result type from a templated binary functor with one template
parameter.
template <template <class> class F, class T, class U >
struct get_result_type
{
typedef typename promote<T,U>::result_type result_type;
};


// given a binary functor F with one template parameter, what template
parameter
// should we instantiate it with if we want to call it on types T and
U ?
template <template <class> class F, class T, class U >
struct get_template_parameter
{
typedef typename promote<T,U>::result_type result_type;
};


template <template <class> class Base, class T > struct X : public
Base<T> {};


template <template <class> class Base>
struct X<Base,void>
{
template <class T>
T operator() (const T& arg)
{
return Base<T>()(arg);
}

template <class T>
T operator() (const T& left, const T& right)
{
return Base<T>()(left,right);
}

template <class T, class U>
typename get_result_type<Base,T,U>::result_type operator()
(const T& left, const U& right)
{
return
Base<get_template_parameter<Base,T,U>::result_type>()
(left,right);
}
};


int main(int argc, char** argv)
{
X<multiplies,double> x;
X<multiplies,void> y;
X<plus,int> z;
X<plus,void> t;


std::cout << x(2,3.4) << std::endl;
std::cout << y(2.,3.4) << std::endl;
std::cout << y(2,3) << std::endl;
std::cout << z(2,3) << std::endl;
std::cout << t(2,3) << std::endl;
std::cout << t(2.1,3.) << std::endl;

// works because of promotes template
std::cout << t(2.1,3) << std::endl;
std::cout << t(2,3.4) << std::endl;

system("PAUSE");

}


Zach
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top