English 中文(简体)
从超负荷功能中提取返回类型
原标题:Extracting the return type from an overloaded function

我想抽出一种功能的返回类型。 问题在于,还有其他功能,名称相同,但签字不同,我无法选择C++。 我知道以下几个方面:成绩——但我从几个方面得出结论,它也面临同样的问题。 我也听到了涉及脱节式的解决办法,但我不知道什么细节。

此时,我利用模板的元数据从功能点的类型中抽取回类型,因为从功能点的类型中抽取功能点类型,而功能点的类型是用于明确功能的。

#include <iostream>

using namespace std;

//  ----

#define resultof(x)     typename ResultOf<typeof(x)>::Type  //  might need a & before x

template <class T>
class ResultOf
{
    public:
        typedef void Type;      //  might need to be T instead of void; see below
};

template <class R>
class ResultOf<R (*) ()>
{
    public:
        typedef R Type;
};

template <class R, class P>
class ResultOf<R (*) (P)>
{
    public:
        typedef R Type;
};

//  ----

class NoDefaultConstructor
{
    public:
        NoDefaultConstructor (int) {}
};


int f ();
int f ()
{
    cout << "f" << endl;
    return 1;
}

double f (int x);
double f (int x)
{
    cout << "f(int)" << endl;
    return x + 2.0;
}

bool f (NoDefaultConstructor);
bool f (NoDefaultConstructor)
{
    cout << "f(const NoDefaultConstructor)" << endl;
    return false;
}

int g ();
int g ()
{
    cout << "g" << endl;
    return 4;
}

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

//  this works since there is no ambiguity. does not work without &
//  resultof(&g) x0 = 1;
//  cout << x0 << endl;

//  does not work since type of f is unknown due to ambiguity. same thing without &
//  resultof(&f) x1 = 1;
//  cout << x1 << endl;

//  does not work since typeof(f()) is int, not a member function pointer; we COULD use T instead of void in the unspecialized class template to make it work. same thing with &
//  resultof(f()) x2 = 1;
//  cout << x2 << endl;

//  does not work per above, and compiler thinks differently from a human about f(int); no idea how to make it correct
//  resultof(f(int)) x3 = 1;
//  cout << x3 << endl;

//  does not work per case 2
//  resultof(f(int())) x4 = 1;
//  cout << x4 << endl;

//  does not work per case 2, and due to the lack of a default constructor
//  resultof(f(NoDefaultConstructor())) x5 = 1;
//  cout << x5 << endl;

//  this works but it does not solve the problem, we need to extract return type from a particular function, not a function type
//  resultof(int(*)(int)) x6 = 1;
//  cout << x6 << endl;

}

任何想法,我没有哪一个星号,以及如何加以固定,最好是采用一种简单方式运作的解决方案,例如resultof(f(int)?

最佳回答

我认为,可以通过<条形码> dec/条码>和<条码> dec/条码>做到这一点:

For example: decltype(f(std::declval<T>())).

问题回答

很难在没有争论的情况下检查超负荷的职能名称。 您可以检查超负荷的返回类型,但这种超重载必须不止一次。 即便如此,将硬差错(如果某一天有不止一个超负荷)转化为SFINAE是一种痛苦,因为它要求只为这一特定功能撰写一个海峡(!),因为超负荷的职能名称不能作为任何理由通过。 视力还要求用户代码使用明确的专业化......

template<typename R>
R
inspect_nullary(R (*)());

template<typename R, typename A0>
R
inspect_unary(R (*)(A0));

int f();
void f(int);

int g();
double g();

typedef decltype(inspect_nullary(f)) nullary_return_type;
typedef decltype(inspect_unary(f)) unary_return_type;

static_assert( std::is_same<nullary_return_type, int>::value, "" );
static_assert( std::is_same<unary_return_type, void>::value, "" );

// hard error: ambiguously overloaded name
// typedef decltype(inspect_nullary(g)) oops;

鉴于你重新使用C++0x,我认为有必要指出,(海事组织)从来没有需要检查超过<密码>的回归类型,即:结果-of<Functor(Args...)>: 类型,而这不适用于职务名称;但也许你对此的兴趣只是学术上的。

Okay,经过几次尝试,我设法围绕<代码>std:declval方法在Mankarse建议下开展工作。 我使用了单程式班级模板来确定参数,并利用模板扣除功能,从一个职能点获得回报价值。 现有星号为<代码> 类型(ResultOf< 参数>:get(Function),但不幸的是,该编码仍然远未达到预期的<代码>resultof< 参数与功能)。 如果我找到办法进一步简化这一答案,那将it。

#include <iostream>
#include <typeinfo>

using namespace std;

template <class... Args>
class ResultOf
{
    public:
        template <class R>
        static R get (R (*) (Args...));
        template <class R, class C>
        static R get (R (C::*) (Args...));
};

class NoDefaultConstructor
{
    public:
        NoDefaultConstructor (int) {}
};

int f ();
double f (int x);
bool f (NoDefaultConstructor);
int f (int x, int y);


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

    cout << typeid(typeof(ResultOf<>::get(f))).name() << endl;
    cout << typeid(typeof(ResultOf<int>::get(f))).name() << endl;
    cout << typeid(typeof(ResultOf<NoDefaultConstructor>::get(f))).name() << endl;
    cout << typeid(typeof(ResultOf<int, int>::get(f))).name() << endl;

    typeof(ResultOf<int>::get(f)) d = 1.1;
    cout << d << endl;
}

Edit:

为了用单体办法解决该问题,代号为<>代号>。 如果没有他们,我就没有把参数类型与模板之间的对应体。 加入<条码>(f)、(第1段、第2段等),无效果。

#include <iostream>

using namespace std;

template <class... Args>
class Param
{
    public:
        template <class R>
        static R Func (R (*) (Args...));
        template <class R, class C>
        static R Func (R (C::*) (Args...));
};

#define resultof(f, ...) typeof(Param<__VA_ARGS__>::Func(f))

int f ();
double f (int x);
int f (int x, int y);

int main (int argc, char* argv[])
{
    resultof(f, int) d = 1.1;
    cout << d << endl;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签