这是所有“分配的左操作数所需的左值”错误问题的反函数。
我有一个重载operator []的类,但是只有返回临时类的版本。如果要返回一个整数:
struct Foo
{
int operator[]( int idx ) const { return int( 0 ); }
};
Foo f;
f[1] = 5;
Run Code Online (Sandbox Code Playgroud)
我会正确地得到左值编译器错误。但是,如果返回结构类型,则编译器(在本例中为GCC 7.2)一点也不抱怨:
struct Bar {};
struct Foo
{
Bar operator[]( int idx ) const { return Bar(); }
};
Foo f;
f[1] = Bar();
Run Code Online (Sandbox Code Playgroud)
如果Bar是临时的并且没有专门的运算符=,为什么不以同样的方式抱怨呢?另一个问题,有什么办法可以使这个抱怨?显然,如果以这种方式使用,这是一个编码错误
我倾向于认为我对C++内部和内存布局有很好的把握,但这个让我感到困惑.我有以下测试代码:
#include <stdio.h>
struct Foo
{
//Foo() {}
int x;
char y;
};
struct Bar : public Foo
{
char z[3];
};
int main()
{
printf( "Foo: %u Bar: %u\n", (unsigned)sizeof( Foo ), (unsigned)sizeof( Bar ) );
}
Run Code Online (Sandbox Code Playgroud)
输出是合理的:
Foo:8 Bar:12
但是,这是非常奇怪的部分,如果我取消注释Foo()上的简单默认构造函数,sizeof(Bar)会发生变化!如何添加ctor可能会改变这些类的内存布局?
Foo:8 Bar:8
使用gcc-7.2编译
我在查找有关GCC的对齐新警告和gcc -faligned-new选项的更多信息时遇到了一些困难.在gcc 7.2.0上编译(没有--std = c ++ 17)并尝试定义一个对齐的结构,例如:
struct alignas(64) Foo { int x; }
Run Code Online (Sandbox Code Playgroud)
做一个简单的旧:
Foo * f = new Foo();
Run Code Online (Sandbox Code Playgroud)
给我以下警告和建议:
alignas.cpp:36:25: warning: ‘new’ of type ‘Foo’ with extended alignment 64 [-Waligned-new=]
Foo * f = new Foo();
^
alignas.cpp:36:25: note: uses ‘void* operator new(long unsigned int)’, which does not have an alignment parameter
alignas.cpp:36:25: note: use ‘-faligned-new’ to enable C++17 over-aligned new support
Run Code Online (Sandbox Code Playgroud)
据我所知,默认情况下new只返回内存对齐alignof( std::max_align_t )(对我而言是16),但我不清楚的是,如果我传递-faligned-new,gcc现在会new代表我强制执行新的对齐吗?
不幸的是,关于这个的gcc文档非常缺乏.
我刚刚遇到这种奇怪的情况,我无法解释它.我真的很好奇,最重要的事情发生了什么.我有这个示例代码:
#include <stdio.h>
#include <stdint.h>
int main()
{
int64_t qty = 900;
double p = 74.45;
printf( "%f|%ld\n", p, qty );
printf( "%f|%ld\n", qty, p );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意,在第二个printf中,我提供了错误订单中的参数,更不用说类型错误了.但是,我仍然得到两个CORRECT输出?奇怪的是......用gcc 7.2编译:
$ ./a.out
74.450000|900
74.450000|900
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我有一些模板类,有两个私有静态成员.用户定义traits结构并将其提供给模板类,然后从模板类派生.
然后在c ++文件中,用户定义静态成员,其中一个成员从另一个成员初始化.出于某种原因,如果我没有完全指定arg的命名空间,我会得到一个"类尚未声明"错误.这只是一个问题,当我在一个嵌套的命名空间时,如果你在一个顶级命名空间中定义类型没有问题,这让我觉得这是一个编译器错误.修剪下面的示例,使用gcc 7.2进行编译
template<typename Traits>
struct Base
{
static int x;
static int y;
};
namespace foo::bar
{
struct BarTraits
{
};
using Bar = Base<BarTraits>;
template<> int Bar::x = 0;
template<> int Bar::y( Bar::x ); //error
//template<> int Bar::y( foo::bar::Bar::x ); //no error
}
Run Code Online (Sandbox Code Playgroud) 我看不到 m 来弄清楚如何迭代接受的 argparse 参数。我知道我可以遍历 parsed_args 结果,但我想要的是遍历解析器配置的参数(即使用 optparse 可以遍历 args )。
例如:
parser = argparse.ArgumentParser( prog = 'myapp' )
parser.add_argument( '--a', .. )
parser.add_argument( '--b', ...)
parser.add_argument( '--c', ... )
for arg in parser.args():
print arg
Run Code Online (Sandbox Code Playgroud)
会导致
--a
--b
--c
Run Code Online (Sandbox Code Playgroud) c++ ×4
gcc ×2
alignas ×1
c ×1
constructor ×1
gcc-warning ×1
inheritance ×1
lvalue ×1
namespaces ×1
printf ×1
python ×1
sizeof ×1