我有这个代码
#include <vector>
#include <iostream>
int main(int argc, char* argv[])
{
std::vector<int> v1,v2;
std::cout << std::distance(v1.begin(),v2.begin());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它有一个bug,因为比较两个不同向量的迭代器没有意义.
我看了一下N3376在24.4.4迭代器操作在815页:
Run Code Online (Sandbox Code Playgroud)template<class InputIterator> typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);要求:如果
InputIterator符合随机访问迭代器的要求,last应可以从first或first到达last; 否则,last应该可以从first.
现在我认为需求没有实现.
在这种情况下,标准状态应该发生什么?
书写是否const auto& [a, b] = f();延长从返回的对象的终身保障f(),或者至少对象a和b必然?通过阅读提案,我没有看到任何明显的语言,以确保它确实存在,除非它只是被其他东西覆盖.但是,以下内容并未延长临时的生命周期,因此我不知道它将如何覆盖:
const auto& a = std::get<0>(f());
Run Code Online (Sandbox Code Playgroud)
在论文的顶部,似乎表明它已被覆盖
分解声明的cv-qualifiers和ref-qualifier应用于为初始化程序引入的引用,而不是单个成员别名
但是在实际标准的拟议措辞中,我看到的最接近的提法如下,但我不知道如何阅读它以获得我正在寻找的保证:
如果e是未分类的id-expression,命名从分解声明的identifier-list引入的左值或引用,则decltype(e)是分解声明规范中给出的引用类型
似乎gcc和clang都延长了返回对象的生命周期,直到基于wandbox实验的范围结束.一个实现我自己类型的所有铃声和口哨声的丑陋似乎延长了外部对象及其他数据成员的生命周期.
虽然几乎可以肯定作者的意图,但我想确切地知道这种语言可以保证这是安全的.
在开发应用程序时,我遇到了以下问题.我想std::list<string>在给定的函数指针为null时返回空,否则返回该函数的结果.这是我的代码的简化版本:
typedef std::list<std::string> (*ParamGenerator)();
std::list<std::string> foo() {
/* ... */
ParamGenerator generator = ...;
if(generator)
return generator();
else
return {};
}
Run Code Online (Sandbox Code Playgroud)
但是,我通常喜欢?:在这些情况下使用ternary()运算符,所以我尝试这样使用它(像往常一样):
return generator ? generator() : {};
Run Code Online (Sandbox Code Playgroud)
但得到了这个错误:
somefile.cpp:143:46: error: expected primary-expression before ‘{’ token
somefile.cpp:143:46: error: expected ‘;’ before ‘{’ token
Run Code Online (Sandbox Code Playgroud)
这是否意味着我不能使用三元运算符来返回使用其构造函数创建的对象initializer_list?这有什么特别的原因吗?
首先,我有一个结构,其中一个值具有默认值
struct S {
int a = 1;
};
Run Code Online (Sandbox Code Playgroud)
当gcc和clang都是非const/non-constexpr时,可以默认构造此类型.在两者之下,std::is_pod<S>::value是false.奇怪的行为如下:
S s1; // works under both
const S s2{}; // works under both
const S s3; // only works in gcc, clang wants a user-provided constructor
Run Code Online (Sandbox Code Playgroud)
以下尝试都没有对clang产生影响:
struct S {
int a = 1;
constexpr S() = default; // defaulted ctor
virtual void f() { } // virtual function, not an aggregate
private:
int b = 2; // private member, really not an aggregate
};
Run Code Online (Sandbox Code Playgroud)
我唯一可以做的就是 …
我可以生成一个线程pthread_create并std::mutex安全地使用它吗?
我认为如果std::mutex实现为a pthread_mutex_t那么它会很好,但我没有看到任何记录
例如:
#include <pthread.h>
#include <mutex>
namespace {
std::mutex global_lock;
}
void* thread_func(void* vp) {
// std::mutex used in thread spawned with pthread_create
std::lock_guard<std::mutex> guard(global_lock);
// critical section
return nullptr;
}
int main() {
pthread_t tid;
pthread_create(&tid, nullptr, thread_func, nullptr);
pthread_join(tid, NULL);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我正在运行Debian Wheezy.
我已经看到了ANSI C编译器必须支持的许多最低要求,如函数的31个参数,并且大多数数字似乎都有某种意义.
但是,我看不到在源代码行中支持至少509个字符的原因.511或512会更有意义,但509似乎是任意的.
这个号码的原因是什么?
我经常发现自己使用std::sort,std::max_element以及与简单地调用成员函数拉姆达等等
std::vector<MyType> vec;
// populate...
auto m = std::max_element(std::begin(vec), std::end(vec),
[](const MyType& a, const MyType& b) { return a.val() < b.val()})
Run Code Online (Sandbox Code Playgroud)
这感觉就像是浪费角色和失去清晰度.我知道我可以编写另一个函数/可调用函数并将函数指针/可调用对象传递给这些算法函数,但我经常需要在程序中执行此类操作,并且它不会让我感觉良好解决问题的方法.我想做什么,理想情况是说:
auto m = std::max_element(std::begin(vec), std::end(vec), &MyType::val);
Run Code Online (Sandbox Code Playgroud)
并按对象对对象进行排序val().我有什么部分stdlib可以帮助我解决这个问题吗?或另一种简单的方法吗?我想尽可能明显地进行排序或搜索.
我知道这&MyType::val还不够,我正在寻找可以包装它的东西,或提供类似的功能而不会妨碍其含义.
我似乎回想起低级语言中的情况,即在程序中多次打开文件可能会导致共享的搜索指针.通过在Python中乱搞一下,这似乎并没有发生在我身上:
$ cat file.txt
first line!
second
third
fourth
and fifth
Run Code Online (Sandbox Code Playgroud)
>>> f1 = open('file.txt')
>>> f2 = open('file.txt')
>>> f1.readline()
'first line!\n'
>>> f2.read()
'first line!\nsecond\nthird\nfourth\nand fifth\n'
>>> f1.readline()
'second\n'
>>> f2.read()
''
>>> f2.seek(0)
>>> f1.readline()
'third\n'
Run Code Online (Sandbox Code Playgroud)
这种行为是否安全?我很难找到一个消息来源说它没关系,如果我可以依赖它,它会有很大帮助.
我没有将该位置视为文件对象的属性,否则我对此更有信心.我知道它可以保存在迭代器内部,但idk如何.tell()在这种情况下会得到它.
>>> dir(f1)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'close', 'closed', 'encoding', 'fileno', 'flush',
'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline',
'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines',
'xreadlines']
Run Code Online (Sandbox Code Playgroud)
更新
在Python基本参考的 …
注意:我知道
with open('f1') as f1, open('f2') as f2:
...
Run Code Online (Sandbox Code Playgroud)
句法.这是一个不同的问题.
给定一个字符串列表file_names有一种方法可以使用with/ as打开每个文件名,使用一行.像这样的东西:
with [open(fn) for fn in file_names] as files:
# use the list of files
Run Code Online (Sandbox Code Playgroud)
这当然不起作用,因为它试图在列表上使用上下文管理器.在运行时之前可能无法知道列表的长度,例如sys.argv[1:]
以下程序尝试使用第一个字符串和指向第一个字符串中间的指针构造第二个字符串:
#include <string>
int main() {
std::string src = "hello world";
const char* end = &src[5];
std::string dest(src.data(), end);
}
Run Code Online (Sandbox Code Playgroud)
在C++ 14及更早版本中,这可行.但是在C++ 17中,调用失败了:
error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(char*, const char*&)’
std::string dest(src.data(), end);
[... full output omitted ...]
Run Code Online (Sandbox Code Playgroud)
改变了什么让这次失败?