使用g ++ 3.4和4.7,我观察到以下奇怪的行为:
如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是.我在C++ 98标准中找不到相应的规则.g ++是否正确,(我假设)?或者这是一个错误?
template <class T>
int x(auto_ptr_ref<T> p)
{
return 1;
}
// this would match
/*
int x(auto_ptr_ref<int> p)
{
return 2;
}
*/
void dummy()
{
cout << x(auto_ptr<int>()) << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ templates function overload-resolution implicit-conversion
我无法理解这个程序的输出:
#include<iostream>
using namespace std;
int main()
{
int x = 1 , y = 1, z = 1;
cout << ( ++x || ++y && ++z ) << endl; //outputs 1;
cout << x << " " << y << " " << z ; //x = 2 , y = 1 , z = 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
2 1 1
Run Code Online (Sandbox Code Playgroud)
如果||被评为第一,然后这个输出是好的,但是这个文章说,&&比一个更高的优先级||,因此必须首先评估.如果是这种情况那么根据我的输出应该是:
1
1 2 2
Run Code Online (Sandbox Code Playgroud)
如++y …
我想给数组写一些字节.为了利用现代C++,我决定使用智能指针.
#include <memory>
#include <cstdint>
using namespace std;
void writeUint32_t(uint32_t value, unsigned char* p){
*p = static_cast<unsigned char>((value >> 24) & 0xFF);
*(++p) = static_cast<unsigned char>((value >> 16) & 0xFF);
*(++p) = static_cast<unsigned char>((value >> 8) & 0xFF);
*(++p) = static_cast<unsigned char>((value ) & 0xFF);
}
int main(){
auto buf = make_shared<unsigned char[]>(512);
uint32_t data = 1234;
writeUint32_t(data, buf.get() + 8);
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下编译错误:
u.cpp:15:37: error: invalid use of array with unspecified bounds
writeUint32_t(data, buf.get() + 8);
^
u.cpp:15:38: error: cannot …Run Code Online (Sandbox Code Playgroud) 我对这两个感到困惑.我知道C++引用本身就是常量,一旦设置它们就无法更改为引用别的东西.
我试过多次这样编码:
struct Foo
{
double const& f;
Foo(double const& fx) : f(fx)
{
printf("%f %f\n", fx, this->f); // 125 125
}
double GetF() const
{
return f;
}
};
int main()
{
Foo p(123.0 + 2.0);
printf("%f\n", p.GetF()); // 0
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它根本不会崩溃。我还使用valgrind来测试程序,但没有出现错误或警告。所以,我假设编译器自动生成了一个代码,将引用指向另一个隐藏变量。但我真的不确定。
鉴于下面的示例,我很惊讶地发现尽管显式删除了默认构造函数(或者默认为此),但仍然可以进行聚合初始化.
#include <iostream>
struct DefaultPrivate
{
const int n_;
static const DefaultPrivate& create();
private:
DefaultPrivate() = delete;
};
const DefaultPrivate& DefaultPrivate::create()
{
static DefaultPrivate result{10};
return result;
}
int main() {
DefaultPrivate x; //Fails
DefaultPrivate y{10};//Works
return 0;
}
Run Code Online (Sandbox Code Playgroud)
私有默认(或删除)构造与聚合初始化之间的关系是否未在标准中指定?
GCC 6.3和VCC 2017就属于这种情况
我问这个问题的原因是,我希望更改对默认构造函数的访问会阻止公共聚合初始化
c++ initialization language-lawyer aggregate-initialization c++11
遇到了一个非常见的c ++初始化代码,似乎可以正常使用以下代码...
struct sfoobar { char bar[10]; char foo[10]; };
...
sfoobar x { 0 };
Run Code Online (Sandbox Code Playgroud)
这是将这些char数组初始化为零的可接受方法吗?
非常简化的示例(不必理会类A和运算符在做什么,仅举例来说):
#include <iostream>
using namespace std;
template <bool is_signed>
class A {
public:
// implicit conversion from int
A(int a) : a_{is_signed ? -a : a}
{}
int a_;
};
bool operator==(A<true> lhs, A<true> rhs) {
return lhs.a_ == rhs.a_;
}
bool operator==(A<false> lhs, A<false> rhs) {
return lhs.a_ == rhs.a_;
}
int main() {
A<true> a1{123};
A<false> a2{123};
cout << (a1 == 123) << endl;
cout << (a2 == 123) << endl;
return 0;
} …Run Code Online (Sandbox Code Playgroud) c++ templates implicit-conversion template-argument-deduction
我试图用std::remove_if它从一个简单的字符串中删除空格,但结果却很奇怪。有人可以帮我弄清楚发生了什么吗?
该代码是:
#include <iostream>
#include <algorithm>
#include <string>
int main(int argc, char * argv[])
{
std::string test = "a b";
std::remove_if(test.begin(), test.end(), isspace);
std::cout << "test : " << test << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这只是打印出来:
test : ab
Run Code Online (Sandbox Code Playgroud)
但是我得到了
test : abb
Run Code Online (Sandbox Code Playgroud)
尝试另一个字符串,我得到:
输入:“ a bcde uv xy”
输出:“ abcdeuvxy xy”
似乎它在复制最后一个“单词”,但有时会添加一个空格。我怎样才能在不做任何奇怪的事情的情况下删除所有空间?
我在这里面临着一个相对棘手的情况,乍一看似乎很容易。将这三个成员从父类移动Parent到其子类后Child,我似乎不再能够利用默认构造函数。
为什么?有没有办法无需专门实现 Child(...) 构造函数。乍一看似乎违反直觉......实际上我认为第一个示例是它失败的地方(认为子类的构造函数会掩盖其父类的构造函数)。
struct Parent
{
std::string mText;
int mInt;
bool mBool;
};
struct Child : public Parent
{
};
Child test{ "", 0, false}; // Compiles
Run Code Online (Sandbox Code Playgroud)
但在后一种情况下,如果在子类中定义了成员,则不会创建默认构造函数。
struct Parent
{
};
struct Child : public Parent
{
std::string mText;
int mInt;
bool mBool;
};
Child test{ "", 0, false}; // error C2078: too many initializers
Run Code Online (Sandbox Code Playgroud) c++ inheritance initialization initializer-list aggregate-initialization