以下内容无法编译:
template<void *p>
class X {
// ...
};
int r;
int main()
{
X<&r> x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息是
x.cc:10:6:错误:无法将模板参数'&r'转换为'void*'
显式地转换&r到(void*)也无济于事.错误消息变为:
x.cc:10:14:错误:无法将模板参数'(void*)(&r)'转换为'void*'
标准的哪一部分指明了这种行为?GCC版本是gcc版本5.2.1 20151003(Ubuntu 5.2.1-21ubuntu2)
编辑:
请注意,使用例如int*而不是void*按预期工作.
编辑:(回答自己)
它并不能一起工作的gcc HEAD 6.0.0 20151016(实验)指定-std = C++ 1Z,既不与隐式也不与显式转换到"无效*"时.
它确实与clang HEAD 3.8.0(主干250513)一起使用,并且在指定--std = c ++ 1z并显式转换为*void*时已经(至少)clang 3.6.0(标记/ RELEASE_360/final)没有明确的演员,clang抱怨如下:
x.cc:10:7:错误:在转换的常量表达式中不允许从'int*'转换为'void*'
负责修复c ++语言规范中的这个bug的是N4268,它已经实现了.
代码
#include <list>
#include <memory>
class B;
class A {
std::list<std::unique_ptr<B>> bs;
public:
A();
~A();
};
int main()
{
A x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然编译.它没有链接因为A::A()
并且A::~A()
缺失,但这是预期的并且没有问题.更改
std::list<std::unique_ptr<B>> bs;
Run Code Online (Sandbox Code Playgroud)
应该称之为std::list
标准构造函数
list() : list(Allocator()) {}
Run Code Online (Sandbox Code Playgroud)
(C++ 14及以上)来
std::list<std::unique_ptr<B>> bs{};
Run Code Online (Sandbox Code Playgroud)
应该调用list(std :: initializer_list,const Allocator&= Allocator());
默认构造函数也是.(感谢Nicol Bolas,他正确地提到[over.match.list] 13.3.1.7)在c ++(Ubuntu 5.2.1-22ubuntu2)5.2.1 20151010和--std = c ++ 17参数中给出以下错误:
/usr/include/c++/5/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]’:
/usr/include/c++/5/bits/unique_ptr.h:236:17: required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() …
Run Code Online (Sandbox Code Playgroud) 我想知道标准的哪些部分在以下代码段中指定:
#include <memory>
class A { };
class B : public A { };
int main()
{
std::unique_ptr<B> bptr = std::make_unique<B>(); // (a)
std::unique_ptr<A> aptr = std::move(bptr); // (b)
std::unique_ptr<A> &aptr_r = bptr; // (c)
std::unique_ptr<A> &&aptr_rr = std::move(bptr); // (d)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(d)编制而且(c)没有编制.请在答案中包含标准的相关部分或适当参考.仅供参考,Ubuntu clang版本3.6.2-1(标签/ RELEASE_362/final)(基于LLVM 3.6.2)给了我
error: non-const lvalue reference to type 'unique_ptr<A>' cannot
bind to a value of unrelated type 'unique_ptr<B>'
std::unique_ptr<A> &aptr_r = bptr;
^ ~~~~
Run Code Online (Sandbox Code Playgroud)
和gcc(Ubuntu 5.2.1-22ubuntu2)5.2.1 20151010给了我
error: invalid initialization …
Run Code Online (Sandbox Code Playgroud) 我正在使用SystemVerilog进行综合.我认为接口数组实际上并不是SystemVerilog中的数组,并且索引必须是一个常量值,但是使用大量的样板generate for
和assign
语句来克服它实际上是语言限制(如果我可以模拟使用更多代码的效果,语言可以做正确的事(Tm)本身).
对于下面的伪代码,为了清楚起见,我遗漏了实际代码(modports,tasks等)中的大部分内容.我有一个界面:
interface i_triplex();
logic a; // input wire
logic b; // output wire
logic [127:0] data; // output wires
endinterface
Run Code Online (Sandbox Code Playgroud)
我将这些接口的数组传递给一个看起来像的模块
module roundrobin_triplex#(
parameter int NUNITS = 8
)
(
input wire clk,
input wire rst,
i_triplex t[NUNITS]
);
always_ff @(posedge clk) begin
if (rst) begin
// insert code to initialize the "data" member
// in all interface instances in the array.
end
else begin
// ...
end
end
endmodule
Run Code Online (Sandbox Code Playgroud)
您在网络中统一使用所有接口实例的首选方式是什么 - 无论其值是NUNITS …