我遇到了一些麻烦constexpr.C++ Primer一书显示了一行代码:
constexpr int sz = size(); // only size() is a constexpr function
// this code is right
Run Code Online (Sandbox Code Playgroud)
然而,这本书没有给出一个具体的例子.所以我自己尝试以下代码:
#include <iostream>
constexpr int fun();
int main()
{
constexpr int f = fun();
std::cout << f << std::endl;
}
constexpr int fun()
{
return 3;
}
Run Code Online (Sandbox Code Playgroud)
但我的编译器说fun()是未定义的.
如果我constexpr改成const,它运行良好,如果我在使用之前更改我的代码以定义constexpr函数:
#include <iostream>
constexpr int fun()
{
return 3;
}
int main()
{
constexpr int f = fun();
std::cout << f << std::endl; …Run Code Online (Sandbox Code Playgroud) stl_construct.h 具有以下功能:
template<typename _T1, typename _T2>
inline void
_Construct(_T1* __p, const _T2& __value)
{
::new(static_cast<void*>(__p)) _T1(__value);
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么铸造__p到void*是必要的,它有什么优势?
这里是代码cpuid2.s:
#cpuid2.s view the cpuid vendor id string using c library calls
.section .data
output:
.asciz "The processor Vendor ID is '%s'\n"
.section .bss
.lcomm buffer, 12
.section .text
.global _start
_start:
movl $0, %eax
cpuid
movl $buffer, %edi
movl %ebx, (%edi)
movl %edx, 4(%edi)
movl %ecx, 8(%edi)
push $buffer
push $output
call printf
addl $8, %esp
push $0
call exit
Run Code Online (Sandbox Code Playgroud)
我按如下方式组装、链接和运行它:
as -o cpuid2.o cpuid2.s
ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
./cpuid2
bash: ./cpuid2: Accessing a …Run Code Online (Sandbox Code Playgroud) 今天当我阅读C++ Primer时,它说类内初始化程序不能使用()我在Stackoverflow上搜索并在这里找到类似的问题.并且接受的答案说:原因可能是声明之间有一个模糊的成员函数和类型成员的定义.但我不完全同意他.我尝试以下代码:
struct Sales_data
{
int i(5); //this line can't be regard as a function
};
Run Code Online (Sandbox Code Playgroud)
但是编译器仍然抱怨.谁能告诉我为什么.\编译器:clang ++版本:3-4
entity timer is
Port ( click : in STD_LOGIC;
clear : out STD_LOGIC;
t_unlock : out STD_LOGIC);
end timer;
architecture Behavioral of timer is
signal temp2 : integer range 0 to 20 := 0;
begin
process
begin
if rising_edge(click) then
temp2<=0;
clear<='0';
t_unlock<='0';
else
temp2<=temp2+1 after 15 ns;
end if;
if temp2=6 then
clear<='1';
elsif temp2=20 then
t_unlock<='1';
end if;
end process;
end Behavioral;
Run Code Online (Sandbox Code Playgroud)
我写了这段代码,编译器说:
Signal temp2 cannot be synthesized, bad synchronous description. The description style you are using to …Run Code Online (Sandbox Code Playgroud)