class parametrized with classes and confusion with helper functions

Viewed 57

I'm learning some new concepts about c++ and I'm playing with them. I wrote some piece of code that really confuses me in terms of how it works.

#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)...
}

The idea here is simple, I have some table with columns. And then I would like to create some functions that require only some set of columns and doesn't care if passed argument has some extra columns.

My confusion is mostly about points (2) and (4) in code... My intuition says it should be the same, why it isn't and (2) compiles and (4) doesn't? Is there any major topic I'm missing and should read up? Is there a way to achieve this particular functionality?

1 Answers
Related