C++ 中 [] 的可变参数运算符重载

Lam*_*ros 0 c++ templates operator-overloading c++11

我希望使用运算符 [] 中传递的表达式。我认为使用变量模板参数可以解决问题,但我错了......在 c++11 中是否有一种方法可以做到这一点?

class object {
 

private:


public:
  void values() { std::cout << "finished" << std::endl; }
  template <typename T, typename... Type> void values(T arg, Type... args) {

    std::cout << arg << "  " << std::endl;
    values(args...);
  }


   template<typename... Type> void operator[](Type... args) {

       values(args...);
  }
};

int main(void) {
  object o1 = object();

  o1.values(1, 6.2, true, "hello"); // Works fine.
  
  o1[1, 6.2, true]; // Only the last value gets printed eg. true
  

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

更广泛的目标是我被要求为此制定一个工作语法

let o3 = object [ values 1, "2", true, -3.14 ];
let o1 = object [  key("x") = -1, key("y") = -2,values 1, "2", true, -3.14 ]; // no commas are missing
Run Code Online (Sandbox Code Playgroud)

在 c++11 中使用 c++11 STL(模板、using、MACROS、运算符重载等)。我正在慢慢地尝试弄清楚如何将它们拼凑在一起

for*_*818 5

首先,您需要了解operator[]只能采用一个参数。您的模板化运算符隐藏了一条非常清楚的错误消息。

struct foo {
    void operator[](int,int);
};
Run Code Online (Sandbox Code Playgroud)

结果出错:

<source>:2:10: error: 'void foo::operator[](int, int)' must have exactly one argument
    2 |     void operator[](int,int);
      |          ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

您可以将其设为可变参数模板,但任何不完全具有一个参数的实例化都是不正确的。


弗兰基,这听起来像是一个棘手的问题。当您编写 C++ 代码时,最好使用 C++ 语法。如果允许使用宏(不应该允许),那么几乎一切皆有可能。您只需要意识到您实际上不再编写 C++ 了。另外,仅仅要求一些语法来编译似乎有点奇怪。我不知道 javascript,也不确定这两行应该意味着什么,所以我只这样做了:让它以某种方式编译。

无论如何,让我们看看能做什么。

let o3 = object [ values 1, "2", true, -3.14 ];
Run Code Online (Sandbox Code Playgroud)

我想这声明o3是一个对象,其初始化程序是从一个名为的容器中检索的object,可以通过values 1, "2", true, -3.14. operator,该行可以通过重载 some和#defineing letto be来编译auto,并values构造一个收集索引的对象(通过其operator,):

对于第二行

let o1 = object [  key("x") = -1, key("y") = -2,values 1, "2", true, -3.14 ]; 
Run Code Online (Sandbox Code Playgroud)

可以使用operator,和滥用类似的技巧key::operator=。我解释key为构造一些键值对,例如key("x") = -1将值映射-1到字符串"x"。它实际上所做的事情对于接下来的所有肮脏的事情来说并不重要。一旦您了解了如何滥用运算符重载,就可以对其进行修改以在细节中执行其他操作。

要查看所有值实际上都传递给operator[]我,我从这里偷了一些元组打印函数: https: //stackoverflow.com/a/41171552/4117728

#include <tuple>
#include <string>
#include <typeinfo>
#include <iostream>
#define let auto
///sf/answers/2882008671/
template<class TupType, size_t... I>
void print(const TupType& _tup, std::index_sequence<I...>)
{
    std::cout << "(";
    (..., (std::cout << (I == 0? "" : ", ") << std::get<I>(_tup)));
    std::cout << ")\n";
}

template<class... T>
void print (const std::tuple<T...>& _tup)
{
    print(_tup, std::make_index_sequence<sizeof...(T)>());
}
//........................................

struct Object {
    template <typename ...T>
    struct ObjectIndex {
        ObjectIndex() {}
        ObjectIndex(std::tuple<T...> ind) : index(ind) {}
        std::tuple<T...> index;        
        template <typename U> 
        ObjectIndex<T...,U> operator,(const U& u){ 
            return { std::tuple_cat(index,std::make_tuple(u)) };
        }
        template <typename...U>
        ObjectIndex<T...,U...> operator,(const ObjectIndex<U...>& other) { 
            return { std::tuple_cat(index,other.index) };
        }
    };
    template <typename ...T>
    int operator[](ObjectIndex<T...> index){
        std::cout << typeid(index.index).name() << "\n";
        print(index.index);
        return 42;
    }
};

struct key {
    std::string k;
    int val;
    key(const std::string& s) : k(s) {}
    Object::ObjectIndex<std::string,int> operator=(int v) {
        val = v;
        return {std::make_tuple(k,val)};
    }

};

#define values Object::ObjectIndex<>{} ,

int main() {
    Object object;
    let o3 = object [ values 1, std::string("2"), true, -3.14 ];
    let o1 = object [  key("x") = -1, key("y") = -2,values 1, std::string("2"), true, -3.14 ]; 
}
Run Code Online (Sandbox Code Playgroud)

现场演示

不要在家里(或其他地方)这样做!

传递字符串文字存在一些问题,我没有费心进一步研究这个问题,所以我允许自己替换"2"std::string("2"). 我想,通过更多地解决无尽的错误消息,这个问题也可以得到解决。

我不知道代码是否接近这两行真正应该做的事情。我只是把它当作一个有趣的练习来以某种方式编译它。当我实际归还一些东西时,operator[]我已经没有想法了。我很难想象一个具有真正可变参数的容器operator[]。我选择永远正确的答案。


TL;DR您可以重载operator[]以获取多个参数吗?不可以。您可以重载它以采用封装任意值的单个参数吗?当然。