c++ - 通过模板成员函数访问私有(private)元组元素

标签 c++ templates c++11 tuples

<分区>

类(class)foo包含一个私有(private)元组成员。我想使用 getElement<I>() 获取对此元组元素的引用.我找到了这个解决方案,但是当对象被传递给另一个类的构造函数时它不起作用 bar :

#include <tuple>

template<class... Args>
class foo {
    std::tuple<Args...> tup_;

    public:
    foo(Args... args) : tup_ {args...} {};

    template<size_t I>
    const typename std::tuple_element<I, std::tuple<Args...>>::type &
    getElement() const {return std::get<I>(tup_);}
};

template<class T>
class bar;

template<class T, class U>
class bar<foo<T,U>> {
    public:
    bar(foo<T,U> f) {
       auto j = f.getElement<0>(); // this is an ERROR!!! Line 22
    }
};


int main()
{
   foo<int, char> f(12,'c');
   auto j = f.getElement<0>(); // but this is OK!

   bar<decltype(f)> b(f);

   return 0;
}

编译器输出:

main.cpp: In constructor 'bar<foo<T, U> >::bar(foo<T, U>)':                                                                                                                                                                      
main.cpp:22:33: error: expected primary-expression before ')' token                                                                                                                                                              
        auto j = f.getElement<0>(); // this is an ERROR!!!                                                                                                                                                                       
                                 ^                                                                                                                                                                                               
main.cpp: In instantiation of 'bar<foo<T, U> >::bar(foo<T, U>) [with T = int; U = char]':                                                                                                                                        
main.cpp:32:24:   required from here                                                                                                                                                                                             
main.cpp:22:29: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'                                                                                                         
        auto j = f.getElement<0>(); // this is an ERROR!!! 

最佳答案

你必须警告编译器 getElement是一个模板方法。为此,您必须指定 template关键字,例如:

f.template getElement<0>()

这是因为否则编译器会尝试将代码解析为 f.getElement < 0以便它尝试调用二进制文件 operator<f.getElement0这不是你想做的。

关于c++ - 通过模板成员函数访问私有(private)元组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795068/

相关文章:

c++ - 不允许从 C++ 中的某些类继承

python - 使用在 python 对象中声明的 c++ std::mutex

c++ - 使用别名引用匿名结构会导致错误

c++ - 将大整数按双倍缩放

c++ - FindCirclesGrid c++11 不起作用,尽管检测器找到了所有点

c++ - 在 Android NDK 中使用多个模块

c++ - 在调用基类之前需要调用成员的方法

c++ - 在临时范围内基于范围的 for 循环

c++ - 我喜欢对我正在玩的这个模板设计理念发表评论

c++ - 方法编译时断言;还是行不通