gpt4 book ai didi

c++ - 在参数和返回类型上重载函数

转载 作者:行者123 更新时间:2023-11-30 05:27:31 25 4
gpt4 key购买 nike

我知道使用结构进行返回类型重载的技巧:

struct function {
operator typeA () { return overloadForTypeA(); }
operator typeB () { return overloadForTypeB(); }
}

问题是这会禁用参数和参数类型重载。所以我一直在尝试使用模板扩展这个概念:

struct function {
//template on the constructor so types are inffered from arguments
template<typename... Args>
funct(Args... arguments) { myArguments = arguments; }

//intermediate storeage of arguments, problem is the template doesn't extend to here
Args myArguments;

//overloads for return type int
int overloadForInt(char* chr) { return 20; }
int overloadForInt(int i) { return 4; }

//overloads for return type char
char overloadForChar(char* chr) { return 'c'; }
char overloadForChar(int i) { return 'i'; }

//implcit cast operators to archive return type overloading
operator int() { return overloadForInt(myArguments...); } //if myArguments doesn't match any overload of overloadForInt compile error should happen
operator char() { return overloadForChar(myArguments...); }
}

如您所见,我遇到了模板未扩展到结构其余部分的问题。有没有办法在整个结构上扩展构造函数模板来解决这个特定问题?或者是否有另一种方法可以在保持参数和参数类型重载的同时将返回类型存档为可重载的?

最佳答案

隐式转换运算符可能会使用 more than 参数调用您的 overloadForXXX,这将导致编译错误。在下文中,我将假设您只对调用构造函数中传递的每个参数的重载感兴趣。请注意 Boost Fusion 库的使用。这个想法是让你的函数类成为一个类模板,然后使用一个辅助函数来创建那个将为你推断参数类型的类。

#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <iostream>



template <class... Args>
class function {

// using a tuple to store the arguments
std::tuple<Args...> m_args;

public:
function(Args... args)
: m_args(std::forward<Args>(args)...){}

private:
struct call_for_int {

void overload(char* chr) {
std::cout << "Called Int overload with char" << std::endl;
}

void overload(int chr) {
std::cout << "Called Int overload with int" << std::endl;
}

template <class T>
void operator()(T&& t) {
overload(std::forward<T>(t));
}
};

struct call_for_char {

void overload(char* chr) {
std::cout << "Called Char overload with char" << std::endl;
}

void overload(int chr) {
std::cout << "Called Char overload with int" << std::endl;
}

template <class T>
void operator()(T&& t) {
overload(std::forward<T>(t));
}
};

public:
// use this to call the char overloads
void call_char() {
auto fun = call_for_char();
boost::fusion::for_each(m_args, std::ref(fun));
}

// use this to call the int overloads
void call_int() {
auto fun = call_for_int();
boost::fusion::for_each(m_args, std::ref(fun));
}
};

// helper function to infer the passed arguments
template <class... Args>
auto make_function(Args&&... args) {
return function<Args...>(std::forward<Args>(args)...);
}

int main() {
auto f = make_function(4, 2, 42);
f.call_char();
f.call_int();
}

关于c++ - 在参数和返回类型上重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37261717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com