- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个结构 Dimensions
,它具有宽度和高度的模板化参数:
template<unsigned W, unsigned H>
struct Dimensions
{
static constexpr unsigned width = W;
static constexpr unsigned height = H;
};
我有 Width
和 Height
类,它们具有各自值的模板化参数:
template<unsigned N>
struct Width
{
static constexpr unsigned value = N;
};
template<unsigned N>
struct Height
{
static constexpr unsigned value = N;
};
我已经为宽度和高度创建了用户定义的文字
template<char... cs>
constexpr auto operator""_w() -> Width<to_unsigned(0, parse(cs)...)>
{
return {};
}
template<char... cs>
constexpr auto operator""_h() -> Height<to_unsigned(0, parse(cs)...)>
{
return {};
}
其中 to_unsigned
和 parse
将字符转换为无符号值
constexpr unsigned to_unsigned(unsigned p)
{
return p;
}
template<class... Ts>
constexpr unsigned to_unsigned(unsigned val, unsigned v, Ts... vs)
{
return to_unsigned(val * 10 + v, vs...);
}
constexpr unsigned parse(char C)
{
return (C >= '0' && C <= '9')
? C - '0'
: throw std::out_of_range("input is not a number");
}
因此,我现在可以创建一个函数模板,它采用从字符串文字创建的 Width
和 Height
实例,并返回一个 Dimensions
template<unsigned W, unsigned H>
constexpr Dimensions<W, H> dimensions(Width<W>, Height<H>)
{
return Dimensions<W, H>{};
}
auto d = dimensions(5_w, 10_h);
static_assert(d.width == 5, "");
static_assert(d.height == 10, "");
我想让用户只能提供一个维度(另一个为 0),或者以不同的顺序提供文字。
我目前实现它的方式是有许多不同的重载:
// width, height
template<unsigned W, unsigned H = 0>
constexpr Dimensions<W, H> dimensions(Width<W>, Height<H>)
{
return Dimensions<W, H>{};
}
// height, width
template<unsigned H, unsigned W = 0>
constexpr Dimensions<W, H> dimensions(Height<H>, Width<W>)
{
return Dimensions<W, H>{};
}
如果我现在添加第三个维度,Breadth
,dimensions
重载的数量将会激增,因为它们是我目前实现的方式,它需要一个重载用于每个可能的排列.
问题:
有没有办法允许用户使用宽度、宽度和高度的任意组合/顺序来指定尺寸、省略某些尺寸等?
(上述代码在 coliru here 上的工作示例)
最佳答案
最直接的方法是从元函数开始搜索特定维度:
template <template <unsigned> class Z, class... Ts>
struct find_dimension;
template <template <unsigned> class Z, class... Ts>
using find_dimension_t = typename find_dimension<Z, Ts...>::type;
// found it
template <template <unsigned> class Z, unsigned N, class... Ts>
struct find_dimension<Z, Z<N>, Ts...> {
using type = Z<N>;
};
// keep going
template <template <unsigned> class Z, class T, class... Ts>
struct find_dimension<Z, T, Ts...>
: find_dimension<Z, Ts...>
{ };
// default to 0
template <template <unsigned> class Z>
struct find_dimension<Z> {
using type = Z<0>;
};
然后我们直接使用它:
template <class... Dimensions>
auto dimensions(Dimensions... ) {
using height = find_dimension_t<Height, Dimensions...>;
using width = find_dimension_t<Width, Dimensions...>;
using breadth = find_dimension_t<Breadth, Dimensions...>;
return Dimensions<width::value, height::value, breadth::value>();
}
一个更聪明的方法可能是利用继承:
template <template <unsigned> class Z, unsigned N>
constexpr unsigned get_dim(Z<N> d) { return N; }
template <template <unsigned> class Z>
constexpr unsigned get_dim(... ) { return 0; }
template<class... DimensionTs>
auto dimensions(DimensionTs...)
{
struct X : DimensionTs... { };
static constexpr X x {};
return Dimensions<
get_dim<Width>(x),
get_dim<Height(x),
get_dim<Breadth>(x)
>();
}
第二个的优点是重复维度是一个编译错误。
关于c++ - 接受用户定义文字的排列时防止过载爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982251/
我正在尝试在 Pyspark 中实现自定义爆炸。我有 4 列,它们是具有几乎相同架构的结构数组(一列结构包含的字段比其他三列少一个)。 对于我的 DataFrame 中的每一行,我有 4 列是结构数组
我如何分解 [group_concat(DISTINCT LineItem.ItemID)] => 600278,WH1502。 我试过了,但什么也没显示 $result = mysqli_query
假设我有一个数据框 df : C1 C2 C3 C4 C5 0 [A] [1] s1 [123]
我的数据在其中一个字段中具有嵌套映射。例如,数据位于名为“customers”的表中,如下所示: 姓名:比尔·琼斯 地址:{"billing":{"street":"123 Main", "city"
我有两个独立的项目(ejb,web),完全独立。问题是我想通过maven将ejb项目添加到web依赖项,但我正在寻找一个爆炸依赖项而不是jar,所以当我编译ejb项目时,更改会立即在web项目中生效。
我正在将我的 ROOT.war 文件复制到 Tomcat 6.0 的 webapps 中。有没有一种方法可以在复制时分解文件。即当我的脚本在 webapps 中复制它时..它应该爆炸并创建 ROOT
我有一个体素世界,类似于 Minecraft。每个体素( block )的大小为 1x1x1。 我想计算具有给定爆炸半径的特定位置的爆炸破坏,这意味着游戏将在该位置周围破坏体素(给定所需的半径)。 意
在我的游戏中有一辆坦克。我有一个扩展 Sprite 的 Barrel 类,这个类负责处理与桶相关的东西,其中之一就是射击时的爆炸动画。这就是我尝试这样做的方式: batch.draw(currShot
我一直在严重依赖 CSS 来创建我正在开发的网站。现在,所有 CSS 样式都在每个标签的基础上应用,所以现在我试图将其移动到更多的外部样式以帮助将来进行任何更改。 但现在的问题是我注意到我收到了“CS
我有一个 NSString:@"1a,1b,1c,1d,5c"。我想把这个 NSString 分成一个 NSMutableArray,但我不知道如何。我认为它相当简单,但我找不到它(也许是因为我的英语
我正在尝试使用 QML Canvas.requestAnimationFrame 来绘制一些自定义动画。我预计所提供的回调每帧调用一次,大约每秒 60 次。我的代码是: Canvas { id
我有: df = pd.DataFrame({'col1': ['asdf', 'xy', 'q'], 'col2': [1, 2, 3]}) col1 col2 0 asdf 1
在对 this question 的回答中MathWorks 支持团队声明: It is also possible to overload the ! (bang) operator by crea
好吧——穿上你 70 年代的喇叭裤,然后走进 WayBack 机器……迪斯科又活了,伙计们……(我怀疑这有标签……) 我需要 UUCP 的继任者。对你们这些 child 来说,在互联网出现之前和拜赛因
我今天阅读的一个问题的答案建议将应用程序部署为展开(解压缩)WAR .这让我开始思考。 我对 JBoss/Tomcat 所做的每一次部署都使用了 WAR/EAR文件。至少在我的内存中。 这些年来我一直
我有一个包含以下内容的 Spark Dataframe: 姓名E1E2E3abc456 我需要各种 E列成为新列中的行,如下所示: 姓名值E类型abc4E1abc5E2abc6E3 This answ
$user = $this->user; $user->name = $request['name']; $user->email = $request
我正在使用 Java 1.6、JBoss 5.1、EJB 3 和 Hibernate 2。每次部署 ear 时,如果 jar 是紧凑型(未展开),应用程序将无法运行。但是,当我炸开 jar 然后将其放
我是 dask 的新手,请多多包涵。 我有一个 JSON 文件,其中每一行都有以下架构: { 'id': 2, 'version': 7.3, 'participants': range(10)
我尝试使用 Chai Should 风格的断言,但是下面的语句让 Mocha 爆炸了。这是我完整的 Require block : /**
我是一名优秀的程序员,十分优秀!