3

My code is supposed is to determine if the given function takes the given type as a parameter. Answering your future "what for" questions I will shortly answer: to use it with boost::enable_if template.

The code uses decltype operator of the C++11. My question is: Is it possible to achieve the same goal using c++03?

#include <iostream>

template <class F, class P>
struct has_arg_of_type
{
    static bool const value = false;
};

template <class R, class A>
struct has_arg_of_type<R (A), A>
{
    static bool const value = true;
};

template <class R, class T, class A>
struct has_arg_of_type<R (T::*)(A), A>
{
    static bool const value = true;
};

int pisz(int);

class MyClass
{
public:
    void pisz(int);
};

int main(int argc, char *argv[])
{

    std::cout << "MyClass::pisz has the int as an argument? " << has_arg_of_type<decltype(&MyClass::pisz), int>::value << std::endl; // Line 32
    std::cout << "pisz has the int as an argument? ? " << has_arg_of_type<decltype(pisz), int>::value << std::endl;
    std::cout << "pisz has the float as an argument? ? " << has_arg_of_type<decltype(pisz), float>::value << std::endl;

    return 0;

}

The error is:

In function 'int main(int, char**)':
Line 32: error: 'MyClass::pisz(int)' cannot appear in a constant-expression
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
user2146414
  • 840
  • 11
  • 29
  • Yes, I know there was an error. I think that codepad (that was originally used by me to pase the code) does not support C++11 yet. However the code compiles on MinGW-32 Qt 5.0 (IIRC g++ 4.6) – user2146414 Mar 26 '13 at 19:41

1 Answers1

2

I think you can do this by means of Boost.FunctionTypes, or also you can use boost type traits.

#include <iostream>
#include <boost/function_types/function_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <boost/typeof/std/utility.hpp>

float pisz(int);

class MyClass
{
public:
    void pisz(int);
};

int main(int argc, char *argv[])
{
    typedef BOOST_TYPEOF(&MyClass::pisz) MyClassPisz;
    typedef BOOST_TYPEOF(pisz) Pisz;

    typedef boost::mpl::at_c<boost::function_types::parameter_types<MyClassPisz>, 1>::type MemberFunction;
    typedef boost::mpl::at_c<boost::function_types::parameter_types<Pisz>, 0>::type Function;

    std::cout << "MyClass::pisz has the int as an argument? " << boost::is_same<MemberFunction, int>::value << std::endl;
    std::cout << "pisz has the int as an argument? ? " << boost::is_same<Function, int>::value << std::endl;
    std::cout << "pisz has the float as an argument? ? " << boost::is_same<Function, float>::value << std::endl;

    return 0;
}
Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39
  • Thanks for reply! I think that the crucial part of your code is BOOST_TYPEOF. I hoped that there is some easy (not involving the boost) way to achieve the same. Nevertheless I can get what I wanted with C++03. – user2146414 Mar 26 '13 at 19:35
  • since it has solved your problem, you could at least mark has solved and upvote!! :) – Hugo Corrá Mar 27 '13 at 12:44
  • I tired to upvote but my reputation is too low:( And I am quite new to the forum: checking your answer as an acceptible one is what you ment? – user2146414 Mar 27 '13 at 21:54