有没有办法std::tie在c ++ 11/1y中编写一个与元组深深联系的变体.也就是说 ,分别tie((x,y),z) = make_tuple(make_tuple(1,2),3)绑定x, y, z到的一个1, 2 and 3,如下例所示.这会很好.谢谢.
#include <tuple>
#include <iostream>
using namespace std;
int main() {
int x, y ,z;
auto t = make_tuple(1,2);
std::tie(y,x)= t;
//std::tie((x,y),z) = make_tuple(t,3); //not working
cout << x << y << z << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Device::SerialPort而不使用裸字 glob,请参阅底部的问题。
这是他们的例子:
$PortObj = tie (*FH, 'Device::SerialPort', $Configuration_File_Name)
print FH "text";
Run Code Online (Sandbox Code Playgroud)
...但是用 *FH 污染命名空间感觉很脏...
我tie(my $fh, ...)像你一样尝试过open (my $fh, ...),但Device::SerialPort没有实现TIESCALAR,所以它给出了一个错误。
这是我的肮脏黑客,但它仍然使用裸字,我什至无法让它限制裸字 glob 的范围:
# This does _not_ scope *FH, but shouldn't it?
{
$port = tie(*FH, 'Device::SerialPort', $ARGV[0]) or die "$ARGV[0]: $!";
$fh = \*FH; # as a GLOB
$fh = bless(\*FH, 'IO::Handle'); # or as an IO::Handle
}
print $fh "text"; # this works
print FH "text"; # …Run Code Online (Sandbox Code Playgroud) 我选择使用领带并找到:
package Galaxy::IO::INI;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {']' => []}; # ini section can never be ']'
tie %{$self},'INIHash';
return bless $self, $class;
}
package INIHash;
use Carp;
require Tie::Hash;
@INIHash::ISA = qw(Tie::StdHash);
sub STORE {
#$_[0]->{$_[1]} = $_[2];
push @{$_[0]->{']'}},$_[1] unless exists $_[0]->{$_[1]};
for (keys %{$_[2]}) {
next if $_ eq '=';
push @{$_[0]->{$_[1]}->{'='}},$_ unless exists $_[0]->{$_[1]}->{$_};
$_[0]->{$_[1]}->{$_}=$_[2]->{$_};
}
$_[0]->{$_[1]}->{'='};
}
Run Code Online (Sandbox Code Playgroud)
如果我删除最后一个"$ [0] - > {$ [1]} …
如果我有一个简单的绑定标量类,每次读取时都会递增,我可以这样做:
package Counter;
use strict;
use warnings;
sub TIESCALAR {
my $class = shift;
my $value = 0;
bless \$value, $class;
return \$value;
}
sub FETCH {
my $self = shift;
my $value = $$self;
$$self++;
return $value;
}
sub STORE {
my $self = shift;
$$self = shift;
}
1;
Run Code Online (Sandbox Code Playgroud)
但是要创建一个我必须使用的计数器变量tie.我可以创建一个计数器并导出它.但我真正想做的就是让它看起来像OO.看来我可以创建一个new这样的方法:
sub new {
my $class = shift;
my $counter;
tie $counter, $class;
return $counter;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的主脚本中得到两个计数器:
my $counter1 = Counter->new();
my $counter2 = Counter->new(); …Run Code Online (Sandbox Code Playgroud) Tie :: IxHash生成一个对象,该对象具有大部分完整的行为集,如数组和散列.但我找不到each函数的等价物,它返回(键,值)对.
我只是忽略了它吗?
如果我必须自己动手,我会认为这样的事情会起作用:
use Tie::IxHash;
$t = Tie::IxHash->new( a,1,b,2,c,3 );
while (($x1, $x2) = map { $_ => $t->Values($_) } $t->Keys ) { say "$x1 => $x2"; }
Run Code Online (Sandbox Code Playgroud)
但输出是一系列无限的
a => 1
Run Code Online (Sandbox Code Playgroud)
......出于我尚不清楚的原因.
任何人都能建议如何each使用绑定哈希?
我一直在修改隐藏绑定数组背后的IPC机制的一些实现细节的想法.目标是能够在服务器端执行类似的操作:
# setup code here. Client provides a function name, we find
# a function to deal with the request:
my $coderef = lookup($method);
local @_;
tie @_, 'My::IPC';
@ret = &$coderef;
Run Code Online (Sandbox Code Playgroud)
然后,My::IPC类将根据需要从管道/套接字读取序列化对象(通过SHIFTor FETCH
方法触发).
我想为服务器函数的作者提供一些方法来编写他可以访问IPC的函数,以便编写本地函数,即:
sub f1 {
while (my $param = shift) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
... 以及 ...
sub f2 {
my ($foo, $bar, $baz, %flags) = @_;
...
}
Run Code Online (Sandbox Code Playgroud)
f1旨在能够处理可能大于可用RAM量的数据流 - 只要每个单独的对象在反序列化后适合RAM,一切都很好.f2
用于"更简单"的函数,其中参数列表可以插入RAM.
为了支持这两种情况下,该TIEARRAYconstuctor和SHIFT,
FETCH和 …
如何隐藏用户的"绑定"调用,以便调用访问者将隐式为他们执行此操作?
我想这样做,因为我有一个可以由用户访问的数据结构,但是可以在用户不知情的情况下修改存储在该结构中的值.
如果数据结构中的属性发生更改,我希望修改引用该属性的任何变量,以便用户始终使用新数据.由于用户总是想要新鲜数据,如果用户甚至不需要知道它正在发生,那么它更简单,更直观.
这是我到目前为止...它似乎没有工作,但输出是:
hello
hello
Run Code Online (Sandbox Code Playgroud)
我想要的是:
hello
goodbye
Run Code Online (Sandbox Code Playgroud)
码:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
{
package File;
use Moose;
has '_text' => (is => 'rw', isa => 'Str', required => 1);
sub text {
my ($self) = @_;
tie my $text, 'FileText', $self;
return $text;
}
}
{
package FileText;
use Tie::Scalar;
sub TIESCALAR {
my ($class, $obj) = @_;
return bless \$obj, $class;
}
sub FETCH {
my ($self) = @_;
return $$self->_text(); …Run Code Online (Sandbox Code Playgroud) 在下面的示例中,如果存在平局,如何要求R选择多个索引(例如,在第3行中,x和y列的最小值均为2.但是,sapply仅为x列提供索引)(如果这看起来像一个天真的问题我很抱歉)
df1< -structure(list(x = c(5, 2, 3), y = c(4, 3, 3)), .Names = c("x",
"y"), row.names = c(NA, -3L), class = "data.frame")
df1
x y
1 5 4
2 2 3
3 3 3
sapply(as.list(rownames(df1)),function(x) which.min(df1[x,]))
y x x
2 1 1
Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能编写一个类似std::tie()函数(使用模板编程),它只能绑定元组的选择组件,并将其他元素绑定到某些占位符std::bind().如果是这样,只需要为他/她感兴趣的部分声明变量.
例如,
std::tie(x,_1,y,_2) = (2,3,4,5);
Run Code Online (Sandbox Code Playgroud) 我在Qt C++的应用程序中有这个代码.我operator==比较结构总是返回false,即使它们相等.我的代码出了什么问题?
这是一个有问题的代码片段:
struct pSettings
{
int speciality;
bool autoCompleteByWord;
bool showChronicConditions;
bool showNavArrows;
bool smallScreenMode;
bool simpleExamination;
bool alwaysSave;
bool inLinePatientList;
double newVisitPrice;
double followVisitprice1;
double followVisitprice2;
double followVisitprice3;
double followVisitprice4;
int autosaveinterval;
bool autoSave ;
bool minimizeToTray;
int selectedTheme;
QString textColor;
QString topBGColor;
QString bottomBGColor;
QString altWinColor;
QString buttonColor;
QString altButtonColor;
QString textBGColor;
QString borderColor1;
QString borderColor2;
QString altButtonColorHover;
QString buttonColorHover;
QString buttonColorDisabled;
QString buttonBorderColorHover;
QString comboBGcolor;
QString buttonTextColor;
QString comboTextColor;
double lOffSet,tOffSet;
QString defaultFont;
double …Run Code Online (Sandbox Code Playgroud) tie ×10
perl ×6
c++ ×3
c++11 ×2
oop ×2
overloading ×2
arrays ×1
c++14 ×1
constructor ×1
each ×1
hash ×1
package ×1
qt ×1
r ×1
serial-port ×1