Topic: Detecting functions types


Author: rani_sharoni@hotmail.com (Rani Sharoni)
Date: Mon, 14 Jan 2002 17:04:00 GMT
Raw View
Hello All:

Can anyone tell if the is_function_or_array template really detects
(only) arrays and functions (not pointer to function) types?

template<typename T>
struct is_function_or_array
{
private:
    typedef char (&yes_type)[1];
    typedef char (&no_type) [2];

    // helper class
    template<typename UU>
    struct Type {};

    //
    // according to 8.3.5/6 - Functions shall not have a return
    // type of type array or function.
    // in those cases it seems like the choose template is not viable
    // because of its second argument.
    //
    template<typename U>
    static no_type  choose(Type<U>, U (*)() = 0);

    static yes_type choose(...);

public:
    static const bool result =
        sizeof(choose(Type<T>())) == sizeof(yes_type);
};

// compile with no errors?
typedef int array[is_function_or_array<void()>::result ? 1 : -1];

// compile with no errors?
typedef int array[is_function_or_array<int[10]>::result ? 1 : -1];

My question is really whether the choose template is not viable when
the template parameter U is function or array type.

Notice that detecting array types is easy, but detecting function
types is more difficult.

Thanks,
Rani

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]