- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数组(任意级别),我想要一个索引运算符:
允许缺少索引,使得以下等价
a(1, 0, 0, 0);
a(1, my::missing);
这本身就很简单(参见下面的示例实现):一个人只是递归地添加 arg * strides[dim]
直到 my::missing
命中。
允许自动在前面添加零,使得以下等价
a(0, 0, 0, 1);
a(1);
这也不难(参见下面的示例实现):递归地添加 arg * strides[dim + offset]
。
我无法理解的是:如何将两者结合起来? 2. 的实现让我从错误的角度开始了 1.(我仅限于 <= C+ +14)
enum class my { missing };
template <size_t dim, class S>
inline size_t index_impl(const S&) noexcept
{
return 0;
}
template <size_t dim, class S, class... Args>
inline size_t index_impl(const S& strides, enum my arg, Args... args) noexcept
{
return 0;
}
template <size_t dim, class S, class Arg, class... Args>
inline size_t index_impl(const S& strides, Arg arg, Args... args) noexcept
{
return arg * strides[dim] + index_impl<dim + 1>(strides, args...);
}
template <class S, class Arg, class... Args>
inline size_t index(const S& strides, Arg arg, Args... args)
{
return index_impl<0>(strides, arg, args...);
}
int main()
{
std::vector<size_t> strides = {8, 4, 2 ,1};
std::cout << index(strides, 1, 2, 0, 0) << std::endl;
std::cout << index(strides, 1, 2, my::missing) << std::endl;
}
template <size_t dim, class S>
inline size_t index_impl(const S&) noexcept
{
return 0;
}
template <size_t dim, class S, class Arg, class... Args>
inline size_t index_impl(const S& strides, Arg arg, Args... args) noexcept
{
return arg * strides[dim] + index_impl<dim + 1>(strides, args...);
}
template <class S, class Arg, class... Args>
inline size_t index(const S& strides, Arg arg, Args... args)
{
constexpr size_t nargs = sizeof...(Args) + 1;
if (nargs == strides.size())
{
return index_impl<0>(strides, arg, args...);
}
else if (nargs < strides.size())
{
return index_impl<0>(strides.cend() - nargs, arg, args...);
}
return index_impl<0>(strides, arg, args...);
}
int main()
{
std::vector<size_t> strides = {8, 4, 2 ,1};
std::cout << index(strides, 1, 2) << std::endl;
std::cout << index(strides, 0, 0, 1, 2) << std::endl;
}
最佳答案
在这个答案的早期版本中,我没有提供完整的实现,因为有些事情对我来说没有意义。
如果此 index
应计算扁平化多维数组的索引,则您的示例实现无效。问题是隐藏的,因为您正在将 index
的两个结果与提供的所有索引进行比较,并缩短假定零填充的版本。
遗憾的是,我在 Catch2 的第一版测试中使用了这种模式。
这是对扁平化多维数组索引的正确测试,其中最后一个索引与扁平化索引匹配:
TEST_CASE("index")
{
std::vector<size_t> strides = { 4, 6, 3, 5 };
SECTION("Padding with leading zeros")
{
constexpr auto i0 = 4;
constexpr auto i1 = 2;
constexpr size_t expected = i0 + i1 * 5;
CHECK(index(strides, 0, 0, i1, i0) == expected);
CHECK(index(strides, 0, 0, i1, i0 - 1) == expected - 1); // last index indexes by one
CHECK(index(strides, i1, i0) == expected);
CHECK(index(strides, i1, i0 - 1) == expected - 1);
}
SECTION("Use my::missing to use padding with tailing zeros")
{
constexpr auto i2 = 4;
constexpr auto i3 = 2;
constexpr size_t expected = (i3 * 6 + i2) * 5 * 3;
CHECK(index(strides, i3, i2, 0, 0) == expected);
CHECK(index(strides, i3, i2, my::missing) == expected);
}
}
现在从您的代码开始并通过这些测试我得到了这个实现:
template <typename T, typename... Ts>
struct last_type_helper {
using type = typename last_type_helper<Ts...>::type;
};
template <typename T>
struct last_type_helper<T> {
using type = T;
};
template <typename... Ts>
using last_type = typename last_type_helper<Ts...>::type;
enum class my { missing };
template <typename... Ts>
constexpr bool LastTypeIsMy = std::is_same<my, last_type<Ts...>>::value;
template <class StrideIter>
size_t index_impl(size_t base, StrideIter)
{
return base;
}
template <class StrideIter>
size_t index_impl(size_t base, StrideIter, my)
{
return base;
}
template <class StrideIter, typename Tn, typename... Ts>
size_t index_impl(size_t base, StrideIter it, Tn xn, Ts... x)
{
return index_impl(base * *it + xn, it + 1, x...);
}
template <class S, class... Args>
size_t index(const S& strides, Args... args)
{
const size_t offset = strides.size() - sizeof...(Args);
const size_t advenceBy = LastTypeIsMy<Args...> ? 0 : offset;
const size_t lastStrides = LastTypeIsMy<Args...> ? offset + 1 : 0;
const size_t tailFactor = std::accumulate(std::end(strides) - lastStrides, std::end(strides),
size_t { 1 }, std::multiplies<> {});
return index_impl(0, std::begin(strides) + advenceBy, args...) * tailFactor;
}
这里是 live demo (通过测试)。
关于c++ - 可变参数模板 : SFINAE on last argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71423523/
例如,如果我的程序名称是 test.c 然后对于以下运行命令,argc = 2 而不是 4。 $test abc pqr* *xyz* 最佳答案 尝试运行: $ echo abc pqr* *xyz*
我正在尝试使用一个容器来显示TextField,但是该容器不喜欢我的操作顺序。这是我的代码: Widget build(BuildContext context) { return Scaffol
我有以下代码: class MetricGoogleGateway extends AMetricGateway{ constructor(id, name, token) {
我像这样调用下面的对象方法。 new Cout( elem1 ).load( 'body' ) new COut( elem1 ).display( 'email' ) 我一次只使用一个实例。因为我一
我正在尝试使用 C++11 中的可变参数函数模板,并通过如下代码了解了基本思想: void helper() { std::cout void helper( T&& arg ) {
在学习 ExtJS 4 时,我发现在定义一个新类时,在 initComponent 中方法可以使用 this.callParent(arguments) 调用父类的构造函数. 我想知道这个 argum
使用 XCode 9,Beta 3。Swift 4。 statsView.createButton("Button name") { [weak self] Void in //stuff st
以下代码将打印1: (function (arguments) { console.log(arguments); }(1, 2)); 实际上,arguments 对象已被覆盖。是否可以恢复函
/** * @param $name * @return Response * @Route ("/afficheN/{name}",name="afficheN") */ public fu
我习惯使用Scala scopt用于命令行选项解析。您可以选择参数是否为 .required()通过调用刚刚显示的函数。 如何定义仅在定义了另一个参数时才需要的参数? 例如,我有一个标志 --writ
所以这是我的代码: def is_valid_move(board, column): '''Returns True if and only if there is an o
我试图在这里运行此代码: threads = [threading.Thread(name='ThreadNumber{}'.format(n),target=SB, args(shoe_type,m
在静态类型函数编程语言(例如 Standard ML、F#、OCaml 和 Haskell)中,编写函数时通常将参数彼此分开,并通过空格与函数名称分开: let add a b = a + b
function validateArguments(args) { if(args.length 2) { throw new RangeError("Invalid amo
我正在使用 Django 1.5 并尝试将参数传递到我的 URL。当我使用前两个参数时,下面的代码工作正常,使用第三个参数时我收到错误。我已经引用了新的 Django 1.5 更新中的 url 用法,
我刚刚开始使用 ember js 并且多次被这个功能绊倒 有人可以简要介绍一下 this._super() 的使用,并解释 ...arguments 的重要性 谢谢 最佳答案 每当您覆盖类/函数(例如
这个问题在这里已经有了答案: How to fix an "Argument passed to call that takes no arguments" error? (2 个答案) 关闭 3
我正在创建一个简单的登录注册应用程序。但是我遇到了错误,我不知道如何解决,请帮忙!这是我的代码: // // ViewController.swift // CHLogbook-Applicati
我是 Swift 的初学者。我尝试创建一个表示 Meal 的简单类。 它有一些属性和一个返回可选的构造函数 但是当我尝试测试它或在任何地方实例化它时,我得到的只是一个错误。似乎无法弄清楚发生了什么。
我有一个在特殊环境下运行其他程序的系统程序: cset shield -e PROGRAM .现在要运行一个 java 程序,我输入了 cset shield -e java PROGRAM ,但这不
我是一名优秀的程序员,十分优秀!