我正在学习一些关于C++的新概念,我正在玩他们。 我写了一段代码,它真的让我对它的工作方式感到困惑。类与类参数化和与帮助函数混淆
#include <iostream>
class aid {
public:
using aid_t = std::string;
void setaid(const std::string& s) {
aid_ = s;
}
const aid_t& getaid() const {
return aid_;
}
private:
aid_t aid_;
};
class c {
public:
using c_t = std::string;
void setc(const aid::aid_t& aid_val) {
if (aid_val.size() < 4)
c_ = "yeah";
else
c_ = aid_val + aid_val;
}
const c_t& getc() {
return c_;
}
private:
c_t c_;
};
template<typename ...Columns>
class table : public Columns... {
};
template <typename... Columns>
void f(table<Columns...>& t) {
t.setaid("second");
std::cout << t.getaid() << "\n";
}
void f2(table<aid>& t) {
t.setaid("third");
std::cout << t.getaid() << "\n";
}
int main() {
table<aid, c> tb;
tb.setaid("first");
std::cout << tb.getaid() << " " << "\n";
// f<c>(tb); // (1) doesnt compile, that seem obvious
f<aid>(tb); // (2) works?
f(tb); // (3) works too -- template parameter deduction
// f2(tb); // (4) doesnt work? worked with (2)...
}
这里的想法很简单,我有一些列的表。然后,我想创建一些函数,只需要一些列的集合,并不在乎如果传递的参数有一些额外的列。 (2)和(4)在代码中...我的直觉认为它应该是相同的,为什么不是这样,(2)编译和(4)不是这样?是否有任何重要的主题我错过了,应该阅读? 有没有办法实现这个特定的功能?干杯