我想定义operator<<使用模板的特化,但我不希望它破坏了这个运算符的行为,如果它已经为某些数据类型定义的话.
enum State {Open, Locked};
enum Input {Coin, Push};
std::string ToString(State c){
switch (c) {
case Locked : return "Locked";
case Open : return "Open";
}
}
std::string ToString(Input c){
switch (c) {
case Coin : return "Coin";
case Push : return "Push";
}
}
template<typename T> //obviously, a bad idea
std::ostream& operator<<(std::ostream& s, T c) {
return s<<ToString(c);
}
Run Code Online (Sandbox Code Playgroud)
稍后在代码中我想使用:
int main() {
std::cout<<Coin<<std::endl;
std::cout<<Open<<std::endl;
std::cout<<std::string("normal string")<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
不出所料,上面给出了编译错误:
error: ambiguous overload for ‘operator<<’ (operand types are …Run Code Online (Sandbox Code Playgroud) 在作用域中创建临时对象时,编译器不会看到构造函数提供的参数并给出编译错误.
如下例所示:
#include <sstream>
#include <iostream>
class csventry
{
public:
csventry(std::ostream& ostream) :
_ostream(ostream)
{}
~csventry()
{ _ostream << std::endl; }
csventry& operator << (const std::string& value) {
_ostream << ", ";
_ostream << value;
return *this;
}
private:
std::ostream& _ostream;
};
int main ()
{
std::ostringstream log;
{ csventry(log) << "User Log-on:"; }
{ csventry(log); }
{ csventry(log) << "Summary:"; }
std::cout<<log.str()<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误.
main.cpp: In function ‘int main()’:
main.cpp:29:16: error: no matching function for call to ‘csventry::csventry()’
{ …Run Code Online (Sandbox Code Playgroud) 我找到了一些无法理解的东西.我认为它应该与函数堆栈和一些未定义的行为有关.
假设我有一个功能工厂模板(傻一个):
template <unsigned int N=10>
std::function<int&&(const int& n)> build_add_function() {
return [](const int& n) -> int&& {std::move(n+N);};
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它缺少非void函数的return语句,因此编译器向我发出警告......奇怪的是它"按预期"工作
int main() {
auto foo = build_add_function();
std::cout << foo(10);
}
Run Code Online (Sandbox Code Playgroud)
主要输出: 20
当然,为了修复代码,我添加了return语句,它给了我一个分段错误
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Run Code Online (Sandbox Code Playgroud)
我对我正在做的事情有一些误解,但我无法理解它.有人会向我解释这里发生了什么吗?我正在使用gcc版本8.0.1
编辑:刚刚测试gcc 4.8.1并使用return语句按预期工作,没有编译错误.
它是编译器的东西吗?
在C++中,如果:
int a = 3;
int* p = &a;
Run Code Online (Sandbox Code Playgroud)
那为什么呢
const int* &pp = p;
Run Code Online (Sandbox Code Playgroud)
不允许,但是
const int* const &pp = p;
Run Code Online (Sandbox Code Playgroud)
被允许?
我有一个结构,里面是一个指向同一结构的函数的指针.现在我需要调用一个指向结构外部函数的指针.我举一个下面的代码示例:
#include <iostream>
struct test {
void (test::*tp)(); // I need to call this pointer-to-function
void t() {
std::cout << "test\n";
}
void init() {
tp = &test::t;
}
void print() {
(this->*tp)();
}
};
void (test::*tp)();
int main() {
test t;
t.init();
t.print();
(t.*tp)(); // segfault, I need to call it
return 0;
}
Run Code Online (Sandbox Code Playgroud) 检查以下代码:
#include <iostream>
using namespace std;
int& foo() {
static int i = 0;
return i;
}
int main() {
cout << &foo() << endl;
cout << &foo << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如你看到的,第一个cout返回值的打印地址foo(),这将是静态变量i里面foo().对于第二个cout我期待那&foo的回报地址foo()功能,如规定在这里:
2)如果操作数是非静态成员的限定名,例如&C :: member,则结果是指向成员函数的prvalue指针或指向类C中类型T的数据成员的指针.注意,&member和C都不是: :member甚至&(C :: member)可用于初始化指向成员的指针.
但令我惊讶的是,这是我的输出:
0x5650dc8dc174
1
Run Code Online (Sandbox Code Playgroud)
第一个是好的,但第二个是1?怎么回事?为了确保我没有弄乱任何东西,我在下面编写了这段代码C:
#include <stdio.h>
int foo() {
}
int main(void) {
printf("%p", &foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下输出:
0x55732bd426f0 …Run Code Online (Sandbox Code Playgroud) 为什么我需要在线*上制作checker指针
template <typename C> static yes test( checker<C, &C::helloworld>* );
为了使编译时间扣除正常工作,输出1 0?
当我删除时*,输出是0 0
#include <iostream>
struct Generic {};
struct Hello
{ int helloworld() { return 0; } };
// SFINAE test
template <typename T>
class has_helloworld
{
typedef char yes;
typedef struct {char _[2];} no;
template <typename C, int (C::*)()> struct checker;
template <typename C> static yes test( checker<C, &C::helloworld>* );
template <typename C> static no test(...);
public:
enum { …Run Code Online (Sandbox Code Playgroud) #include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
Run Code Online (Sandbox Code Playgroud)
当我用a调用update时const char *,编译器使用bool参数而不是string_view?!调用函数。为什么??
class A{
public: virtual void getName() = 0;
};
class B: public A{
public:
B(int a){
cout<< "B Name : " << a <<endl;
}
void getName() override { cout << "Class B" << endl; }
};
class C: public B{
public:
C(int a){
cout<< "C Name : " << a <<endl;
}
void getName() override { cout << "Class C" << endl; }
};
int main()
{
B *b = new B(10);
b->getName();
C *c = new C(20);
c->getName(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查文件是否成功打开。我发现这种方法可以打开要读取的文件:
char path[]="data/configuration.txt";
std::ifstream configFile(path, std::ifstream::in);
if(configFile) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是究竟要if检查什么?
因为我还发现了以下检查文件是否打开的方法:
char path[]="data/configuration.txt";
std::ifstream configFile;
configFile.open(path, std::ifstream::in);
if(configFile.is_open()) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我还有其他问题。例如,两种打开文件的方法有什么区别?另外,这两个if条件有什么区别?
我认为这些是相似的方法,最终会得到相同的结果,因为我可以使用两种打开方法std::ifstream一样的is_open方法:
std::ifstream configFile(path, std::ifstream::in);
configFile.open(path, std::ifstream::in);
Run Code Online (Sandbox Code Playgroud)