小编my_*_*ion的帖子

占位符可以与ref()一起使用

我正在使用c ++ 11功能,只是尝试了以下代码:

void dump(ostream &os, const MyType &mt)
{

}

void f(const vector<MyType> &mts, ostream &os)
{
   for_each(mts.begin(), mts.end(), bind(dump, ref(os), ref(_1));
}
Run Code Online (Sandbox Code Playgroud)

这段代码在clang编译错误:

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:4417:2: error: no matching function for call to object of type
      'std::_Bind<void (*(std::reference_wrapper<std::basic_ostream<char> >, std::reference_wrapper<const std::_Placeholder<1> >))(std::basic_ostream<char>
      &, const MyType &)>'
        __f(*__first);
Run Code Online (Sandbox Code Playgroud)

如果我剥ref_1,其精编:

for_each(mts.begin(), mts.end(), bind(dump, ref(os), _1);
Run Code Online (Sandbox Code Playgroud)

看起来像占位符不带参考,但只是想确认,你能列出语义定义为什么它没有?它实际上是通过引用传递的吗?

c++ c++11

3
推荐指数
1
解决办法
623
查看次数

逻辑非运算符

来自C/C++的土地,我想知道为什么以下不起作用:

set a 111
if {! $a eq {} } {
  puts hi
}
Run Code Online (Sandbox Code Playgroud)

我知道,如果我改变第二行,if { $a ne {} } {那么它很好,但不能包围我为什么"!" 不起作用.

tcl

3
推荐指数
1
解决办法
3201
查看次数

TCL数组索引 - 规范是什么

我碰巧碰到了这个:

set a(()) 100
Run Code Online (Sandbox Code Playgroud)

它由TCL解释器获取,并向数组添加a索引为()且值为100的元素.

我很困惑.该官员TCL规范并没有说太多关于数组的索引,它可以采取任何字符.那样就好.问题是,如何检索它?

puts $a(()) 不起作用:

can't read "a(()": no such element in array
Run Code Online (Sandbox Code Playgroud)

解决方法是:

set i ()
puts $a($i)
Run Code Online (Sandbox Code Playgroud)

要么

puts [set a(())]
Run Code Online (Sandbox Code Playgroud)

我打败自己是为了理解为什么$a(())当" a(())"被愉快地采取时却不起作用,但找不到任何东西.

你能救吗?或者这是我们刚认识并与之共存的东西?

tcl

3
推荐指数
1
解决办法
278
查看次数

如何添加描述性字符串来断言

我喜欢在断言失败时看到一些有意义的描述。

这是我的代码及其执行:

>cat /tmp/1.py
a="aaa" + "bbb"
print(a)
assert ("hello" + a) and 0

>python /tmp/1.py
aaabbb
Traceback (most recent call last):
  File "/tmp/1.py", line 3, in <module>
    assert ("hello" + a) and 0
AssertionError
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 3.7。

您知道为什么"hello" + a不首先将其评估为字符串连接吗?我该怎么做呢?

[ 更新 ]感谢您的所有答复,这是我在寻找的内容:

>cat /tmp/1.py
a="aaa" + "bbb"
print(a)
assert 0, "hello" + a
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
1
解决办法
56
查看次数

这些代码片段有什么区别?

我有以下代码片段运行良好:

char* head = str;
char* tail = head;
while ( *tail ) {
    ++tail;
}
Run Code Online (Sandbox Code Playgroud)

我更改了while循环以简化,新代码是

char* head = str;
char* tail = head;
while ( *tail++ );
Run Code Online (Sandbox Code Playgroud)

我相信上面两个代码片段的工作方式相同.但第二个呢!在GDB中我看到,对于一个32个字符的字符串,指针尾部比头部大33,应该是31.

我真的很困惑.

c

2
推荐指数
1
解决办法
146
查看次数

如何返回对包含nullptr的unique_ptr的引用

这是我的代码:

typedef map<string, unique_ptr<MyClass> > DB;

const unique_ptr<MyClass>>& find_it(const string &key, const DB &db)
{
  auto itr = db.find(key);
  if (itr == db.end()) {
    return nullptr;
  }
  return itr->second;
}
Run Code Online (Sandbox Code Playgroud)

返回语句会导致编译器警告: returning reference to local temporary object [-Wreturn-stack-address].

是的,我可以理解返回对本地临时变量的引用是不好的,但我想知道这里最简单的修复是什么,给出以下内容:

1. Do not expose the map to the callers of find_it (I use typedef here is just for asking this question, in real code it is wrapped inside an interface).
2. Using an iterator kind of thing to indicate the position …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++11

2
推荐指数
1
解决办法
2382
查看次数

为什么expr $ a eq $ b因"无效的裸字"而失败?

我有TCL 8.6,以下代码工作正常:

set a abc
set b abcd
if {$a eq $b} {puts hi}
Run Code Online (Sandbox Code Playgroud)

但以下让我错误:

set a abc
set b abcd
expr $a eq $b

invalid bareword "abc"
in expression "abc eq abcd";
should be "$abc" or "{abc}" or "abc(...)" or ...
Run Code Online (Sandbox Code Playgroud)

我想知道发生了什么事?这不是if命令中的条件表达式与expr命令中的表达式相同吗?

tcl

2
推荐指数
1
解决办法
924
查看次数

Python tuple() :什么时候重新排序?

我正在使用 Python 3.7 并且对 tuple() 感到困惑。有时它会重新排序数据,有时不会:

>>> a=tuple([2, 1, 3])
>>> print(a)
(2, 1, 3)   <== the tuple from list is not re-ordered

>>> s={2, 1, 3}
>>> b=tuple(s)
>>> print(b)
(1, 2, 3)   <== the tuple from set is re-ordered

>>> print(tuple({10, 5, 30}))
(10, 5, 30)  <== the tuple from set is not re-ordered

>>> print(s)
{1, 2, 3}    <== the set itself is re-ordered
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. tuple() 的预期行为是什么:

    1.1 生成有序元组?

    1.2 修改输入?

  2. 我在哪里可以找到最终文档?我检查了 Python 在线文档https://docs.python.org/3/library/stdtypes.html#tuple,它根本不提供此类信息。

谢谢。

python set

1
推荐指数
1
解决办法
109
查看次数

不能在最新的 C 表达式中使用未命名的结构文字?

这是我的代码:

\n
void f(void)\n{\n    typedef struct Position {\n        int x;\n        int y;\n    } Position;\n\n    Position p1 = { 10UL, 10UL };\n    if (p1 == (Position){10UL, 10UL}) {}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

gcc 给我这个编译错误:

\n
t.c: In function \xe2\x80\x98f\xe2\x80\x99:\nt.c:9:12: error: invalid operands to binary == (have \xe2\x80\x98Position\xe2\x80\x99 {aka \xe2\x80\x98struct Position\xe2\x80\x99} and \xe2\x80\x98Position\xe2\x80\x99 {aka \xe2\x80\x98struct Position\xe2\x80\x99})\n    9 |     if (p1 == (Position){10UL, 10UL}) {}\n      |            ^~\n
Run Code Online (Sandbox Code Playgroud)\n

看起来我不能在表达式中使用未命名的结构文字?在这个文档中,https://en.cppreference.com/w/c/language/compound_literal,我没有看到有这样的限制。

\n

你能指出我使用上面的结构文字有什么问题吗?

\n

我正在使用 gcc 9.4.0。我可以看到它支持以下C标准:\nc11\nc17\nc18\nc1x\nc2x\nc89\nc90\nc99\nc9x

\n

c

1
推荐指数
1
解决办法
150
查看次数

我可以使用绑定的流操作符吗?

我还在学习关于如何正确使用bind的c ++ 11功能.这是一个实验:

using namespace std::placeholders;
using namespace std;

struct MyType {};

ostream& operator<<(ostream &os, const MyType &n)
{
    os << n;
    return os;
}

int main()
{
    std::vector<MyType> vec;

    std::for_each(vec.begin(), vec.end(), std::bind(operator<<, std::ref(std::cout), _1));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了clang编译错误:

error: no matching function for call to 'bind'
    std::for_each(vec.begin(), vec.end(), std::bind(operator<<, std::ref(std::cout), _1));
Run Code Online (Sandbox Code Playgroud)

我猜绑定无法区分我的文件中定义的函数operator <<和那些预定义的函数.

但我想知道它是否真的可以完成而且我做错了?

[编辑]感谢ISARANDI,前缀::修复了问题.但是在同一个命名空间我有多重函数:

using namespace std::placeholders;
using namespace std;

struct MyType {};
struct MyType2 {};

ostream& operator<<(ostream &os, const MyType &n)
{
    os << n;
    return os; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

0
推荐指数
1
解决办法
272
查看次数

c ++ 11:为什么"auto&"没有推断出正确的类型?

我有以下代码:

struct MyType{};
using vec_type = std::vector<std::unique_ptr<MyType>>;
void foo(vec_type vec, vec_type& vec2, vec_type::iterator itr){
  for (auto &ri = vec.rbegin(); ri != vec.rend(); ++ri) {
    vec2.insert(itr, std::move(*ri));
  }    
}
Run Code Online (Sandbox Code Playgroud)

它在for循环行中收到以下错误:

non-const lvalue reference to type 'reverse_iterator<[...]>' cannot bind to a temporary of type 'reverse_iterator<[...]>'
Run Code Online (Sandbox Code Playgroud)

然后我需要更改为以下内容才能进行编译:

vector<unique_ptr<Titem> >::reverse_iterator ri = replaces.rbegin();
for (; ri != vec.rend(); ++ri) {
    vec2.insert(itr, std::move(*ri));
}    
Run Code Online (Sandbox Code Playgroud)

这对我没有意义 - 我没有看到两个代码之间有任何语义差异.编译器不应该只是推断出"自动"是vector<unique_ptr<Titem> >::reverse_iterator什么?

我正在使用clang ++ 3.5.

c++ type-inference c++11

0
推荐指数
1
解决办法
189
查看次数

标签 统计

c++ ×4

c++11 ×4

tcl ×3

c ×2

python ×2

python-3.x ×1

set ×1

type-inference ×1

unique-ptr ×1