将成员函数传递给模板函数时出现语法错误
Getting syntax error upon passing the member function to template function
首先,我使用 C++ 17 标准。
我遇到问题的代码工作正常,除非我尝试在具有相同模板函数的 class 中使用它。
下面一行代码:
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{}, &lzuint<T, A...>::helper);
导致像 "t could not be initialized" 这样的 10 个编译错误,坦率地说,我看不出原因。
我之前的尝试是使用 lambda 函数而不是辅助成员函数,这也导致了不可读的错误。
这里我提供最小代码:
#include <iostream>
#include <vector>
#include <functional>
#include <string>
#include <tuple>
template<typename _func, size_t... I>
auto make_tuple_seq(std::index_sequence<I...>, _func&& func)
{
return std::make_tuple(func(I)...);
}
constexpr const auto numArgs = 2;
template<typename T, typename... A>
class lzuint
{
protected:
size_t helper(size_t i)
{
return this->body.size() - numArgs + i;
}
public:
lzuint(const std::function<T(A...)>& func, A... args) : f(func), body({ args... }) {}
const uint32_t& operator[](size_t index)
{
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{}, &lzuint<T, A...>::helper);
while (body.size() - 1 < index)
body.push_back(std::apply(f, std::move(t)));
return body[index];
}
private:
std::vector<T> body;
std::function<T(A...)> f;
};
using ullong = unsigned long long;
int main()
{
auto tup = make_tuple_seq(std::make_index_sequence<N>{}, [&v](size_t i) {return v[i]; });//Note:this one works just fine
lzuint<uint32_t, uint32_t, uint32_t> lzu([](uint32_t i, uint32_t j) { return i + j; }, 1, 1);
lzu[1];
lzu[10];
lzu[11];
lzu[12];
lzu[13];
return 0;
}
任何帮助将不胜感激,因为我目前正在尝试通过创建类似于 "lazy evaluation" 技术的模拟来了解可变参数模板。
我遇到的第一个错误是
source.cpp(19): error C2064: term does not evaluate to a function taking 1 arguments
lzuint<T, A...>::helper
是一个非静态成员函数。它需要一个对象来调用。该对象(成为函数内部的 this
指针)通常作为隐藏的第一个参数传递,因此函数不接受一个参数的消息。
有两种方法可以解决这个问题:要么使用 lambdas
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{},
[this](size_t i) { return helper(i); });
或使用std::bind
:
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{},
std::bind(&lzuint<T, A...>::helper, this, std::placeholders::_1));
通常推荐使用 lambda 表达式。
首先,我使用 C++ 17 标准。
我遇到问题的代码工作正常,除非我尝试在具有相同模板函数的 class 中使用它。
下面一行代码:
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{}, &lzuint<T, A...>::helper);
导致像 "t could not be initialized" 这样的 10 个编译错误,坦率地说,我看不出原因。
我之前的尝试是使用 lambda 函数而不是辅助成员函数,这也导致了不可读的错误。
这里我提供最小代码:
#include <iostream>
#include <vector>
#include <functional>
#include <string>
#include <tuple>
template<typename _func, size_t... I>
auto make_tuple_seq(std::index_sequence<I...>, _func&& func)
{
return std::make_tuple(func(I)...);
}
constexpr const auto numArgs = 2;
template<typename T, typename... A>
class lzuint
{
protected:
size_t helper(size_t i)
{
return this->body.size() - numArgs + i;
}
public:
lzuint(const std::function<T(A...)>& func, A... args) : f(func), body({ args... }) {}
const uint32_t& operator[](size_t index)
{
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{}, &lzuint<T, A...>::helper);
while (body.size() - 1 < index)
body.push_back(std::apply(f, std::move(t)));
return body[index];
}
private:
std::vector<T> body;
std::function<T(A...)> f;
};
using ullong = unsigned long long;
int main()
{
auto tup = make_tuple_seq(std::make_index_sequence<N>{}, [&v](size_t i) {return v[i]; });//Note:this one works just fine
lzuint<uint32_t, uint32_t, uint32_t> lzu([](uint32_t i, uint32_t j) { return i + j; }, 1, 1);
lzu[1];
lzu[10];
lzu[11];
lzu[12];
lzu[13];
return 0;
}
任何帮助将不胜感激,因为我目前正在尝试通过创建类似于 "lazy evaluation" 技术的模拟来了解可变参数模板。
我遇到的第一个错误是
source.cpp(19): error C2064: term does not evaluate to a function taking 1 arguments
lzuint<T, A...>::helper
是一个非静态成员函数。它需要一个对象来调用。该对象(成为函数内部的 this
指针)通常作为隐藏的第一个参数传递,因此函数不接受一个参数的消息。
有两种方法可以解决这个问题:要么使用 lambdas
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{},
[this](size_t i) { return helper(i); });
或使用std::bind
:
auto t = make_tuple_seq(std::make_index_sequence<numArgs>{},
std::bind(&lzuint<T, A...>::helper, this, std::placeholders::_1));
通常推荐使用 lambda 表达式。