我偶然发现了这段代码.
std::ostringstream str;
/// (some usage)
assert( ! str );
Run Code Online (Sandbox Code Playgroud)
ostringstream在bool上下文中使用时表示什么?
这可能是编译和运行时不正确的用法吗?
#include <iostream>
#include <cstdlib>
using std::cout;
class A
{
public :
A() { cout << "A()" << this << "\n";}
~A() { cout << "~A()" << this << "\n";}
//void func() { }
virtual void debug(int a) { cout << "A::debug";}
private :
int a;
};
class A1 : public A
{
public :
A1() { cout << "A1()"<< this << "\n";}
~A1() { cout << "~A1()"<< this << "\n";}
private :
int a1;
};
class A2 : public A …Run Code Online (Sandbox Code Playgroud) class Test {
private:
int value;
public:
void display(void)
{
cout << "Value [" << value << "]" << endl;
}
explicit Test(int i)
{
value=i;
}
};
int main() {
Test a(5);
Test b(4.9);
a.display();
b.display();
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使提到了显式,浮点值也会转换为int.
我期望(错误地)浮点数不会转换为整数而对象b不会被构造.
当我在C++ 03上磨牙时,我学会了几种编写"给我收集东西"功能的方法.但每个都有一些挫折.
template< typename Container >
void make_collection( std::insert_iterator<Container> );
Run Code Online (Sandbox Code Playgroud)
要么:
void make_collection( std::vector<Thing> & );
Run Code Online (Sandbox Code Playgroud)
要么:
std::vector<Thing> make_collection();
Run Code Online (Sandbox Code Playgroud)
使用现代C++标准,是否有一个更加惯用的函数接口来"生成一个填充的容器"?
以下是Boost库文档中给出的代码.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!\n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.async_wait(print);
io.run();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在当我运行上面的程序时,它只等待5秒然后打印Hello World并停止.我希望这个程序每5秒钟继续打印Hello World.可能吗 ?
考虑以下代码:
#include <stdio.h>
// =============================
class Shape{
public:
virtual ~Shape(){};
virtual void process() = 0;
};
class Triangle : public Shape{
public:
virtual void process() override {
printf("BBB\n");
}
};
// =============================
/* option 1 */
class TriangleProducer{
public:
Triangle factory(){
return Triangle {};
}
};
/* option 2 */
class PtrShapeProducer{
public:
Shape *factory(){
return new Triangle {};
}
};
/* option 3 */
class PimplShape : public Shape{
Shape *sh;
public:
PimplShape(Shape *sh) : sh(sh){
}
virtual ~PimplShape() …Run Code Online (Sandbox Code Playgroud) 据我所知,inlineC++ 中的关键字可以追溯到旧编译器(当时称为“优化编译器”),它们无法像现代编译器那样进行优化,因此将函数标记为inline告诉编译器这应该是内联,并且作为副作用可以防止 ODR 问题。随着编译器变得更好,有人意识到编译器可以比程序员做得更好,因此inline编译器的要求更多地成为大多数(全部?)现代编译器忽略的“提示”。
输入C++11及后续版本。constexpr在我看来,至少在某些用途上,特别是函数和变量,情况类似。据我了解,它告诉编译器可以在编译时评估某个函数。但这是编译器应该能够自己解决的问题。一旦编译器在优化方面做得更好,这个功能是否也会成为一个“提示”?
注意:我不是询问 的其他用途constexpr,例如 withif语句。我明白这些是需要的。
以以下最小示例为例:
#include <stdio.h>
bool test(){
for (int i = 0; i < 1024; i++)
{
printf("i=%d\n", i);
}
}
int main(){
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test函数中的 return 语句丢失的地方。如果我像这样运行示例:
g++ main.cpp -o main && ./main
Run Code Online (Sandbox Code Playgroud)
然后循环在 1024 次迭代后中止。但是,如果我在打开优化的情况下运行示例:
g++ -O3 main.cpp -o main && ./main
Run Code Online (Sandbox Code Playgroud)
然后这是优化的,我得到一个无限循环。
此行为在g++version10.3.1和clang++version之间保持一致10.0.1。如果我添加 return 语句或将函数的返回类型更改为void.
我很好奇:这会被认为是编译器错误吗?或者这是可以接受的,因为缺少 return 语句是未定义的行为,因此我们失去了对这个函数中发生的事情的所有保证?
class A{
public:
void printer(){
B obj;
obj.private_data = 10; // <- fails, says member inaccessible
}
}
class B{
friend void A::printer();
private:
int private_data;
}
Run Code Online (Sandbox Code Playgroud)
打印机功能是否可以访问 B 类的私有成员?我试图将 B 的 obj 作为 arg 传递给打印机,但它仍然失败
如何计算给定的表达式(function<void()> == functor)。
代码示例:
#include <functional>
using namespace std;
void Foo() { }
int main() {
function<void()> a;
// if (a == Foo) -> error
}
Run Code Online (Sandbox Code Playgroud)
编辑:调试细节,并删除图片。
c++ ×10
c++11 ×2
boolean ×1
boost ×1
boost-asio ×1
boost-thread ×1
c++14 ×1
c++17 ×1
casting ×1
class ×1
polymorphism ×1
stream ×1