为什么s1()在这里调用构造函数?
#include<iostream>
struct s1 {
s1(int tmp) {
}
s1() {
std::printf("s1 invoked");
}
};
struct s2 {
s1 s;
s2(s1& s) {
this->s = s;
}
};
int main() {
s1 o1(5);
s2 o2(o1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
s1 invoked
为什么在s2(s1& s)调用s1()构造函数中按引用传递?
我的任务是创建一个与以下相同的函数
template <typename It>
auto MakeSet(It range_begin, It range_end) {
return set(range_begin, range_end);
}
Run Code Online (Sandbox Code Playgroud)
但它必须是一个模板,它接收迭代器(begin() 和 end())并返回一个包含范围内元素的向量。
我试过
template<typename Type, typename It>
auto MakeVector(It range_begin, It range_end) {
return vector<Type> v(range_begin, range_end);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误
无法识别的模板声明/定义
我在编译 BoringSSL 时发现 gcc 和 clang 之间的行为差异,并且能够将其缩减为以下测试用例来说明:
\ntypedef char *OPENSSL_STRING;\n#if USE_TYPEDEF\n#define constptr const OPENSSL_STRING\n#else\n#define constptr const char *\n#endif\n\nint\nfoo (const void **ap)\n{\n constptr a = (constptr) *ap;\n return a != 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n我测试了四种场景,如下:
\nsh$ g++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF\nt2.cc: In function \xe2\x80\x98int foo(const void**)\xe2\x80\x99:\nt2.cc:11:30: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]\n 11 | constptr a = (constptr) *ap;\n | ^~\nsh$ g++ -c t2.cc -Wignored-qualifiers \nsh$ clang++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF\nsh$ clang++ -c t2.cc -Wignored-qualifiers \nsh$ \nRun Code Online (Sandbox Code Playgroud)\n … 我可以在没有默认初始化的情况下初始化数组的 std::unique_ptr (我只想让它有虚拟值)
我正在尝试使用数组的 unique_ptr 来防止内存泄漏。
但它看起来总是用 0 初始化值(看“movups XMMWORD PTR [rax+48], xmm0”)我不需要这个。我只希望 vector2 具有虚拟值。
我可以这样做吗??
#include <memory>
struct Vector2
{
float x, y;
};
struct A
{
std::unique_ptr<Vector2[]> vector2;
};
int main()
{
A a;
a.vector2 = std::make_unique<Vector2[]>(10);
}
sub rsp, 40 ; 00000028H
mov QWORD PTR a$[rsp], 0
mov ecx, 80 ; 00000050H
call void * operator new[](unsigned __int64) ; operator new[]
test rax, rax
je SHORT $LN15@main
xorps xmm0, xmm0
movups XMMWORD PTR [rax], xmm0
movups XMMWORD …Run Code Online (Sandbox Code Playgroud) 我试图在编译时确定特定类型是否为std::pair类型。当我编译下面的代码时,我在两个分支(即“HERE1”和“HERE2”)上的断言都失败了。如果我删除static_asserts并取消打印,我得到了我期待:那就是“HERE1”为is_pair_type <T :: VALUE_TYPE>和“HERE2”为is_pair_type <T> 。
我想这意味着编译器无法在编译时评估表达式,但我不明白为什么。
使用:MS VS2019,MSVC 版本 14.29.30037
谢谢。
template< class T > struct is_pair : std::false_type {};
template< class T1, class T2 > struct is_pair< std::pair< T1, T2 > > : std::true_type {};
template< class T > struct is_pair_d : is_pair<typename std::decay<T>::type> {};
// a helper function for value
template<class T> struct is_pair_type {
static constexpr bool const value = is_pair_d<T>::value;
};
int main()
{
using T = std::map<int, float>;
T …Run Code Online (Sandbox Code Playgroud) c++ static-assert type-traits c++17 compile-time-type-checking
我有一个 Vector3 类,它的 + 和 * 运算符重载如下:
Vector3& operator+ (Vector3& v1, Vector3& v2)
{
Vector3 sum = Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
static Vector3& ref = sum;
return ref;
}
Vector3& operator* (Vector3& v1, double value)
{
Vector3 product = Vector3(v1.x * value, v1.y * value, v1.z * value);
static Vector3& ref = product;
return ref;
}
Vector3& operator* (double value, Vector3& v1)
{
Vector3 product = Vector3(v1.x * value, v1.y * value, v1.z * value); …Run Code Online (Sandbox Code Playgroud) 我正在尝试以下...
#include <iostream>
using namespace std;
template<class T>
class Singleton
{
private:
class InstPtr
{
public:
InstPtr() : m_ptr(0) {}
~InstPtr() { delete m_ptr; }
T* get() { return m_ptr; }
void set(T* p)
{
if (p != 0)
{
delete m_ptr;
m_ptr = p;
}
}
private:
T* m_ptr;
};
static InstPtr ptr;
Singleton();
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
static T* instance()
{
if (ptr.get() == 0)
{
ptr.set(new T());
}
return ptr.get();
}
};
class ABC
{ …Run Code Online (Sandbox Code Playgroud) c++ templates definition template-specialization explicit-specialization
class TrieNode {
public:
TrieNode() : next{new TrieNode *[26]} {}
TrieNode **next;
string word;
};
Run Code Online (Sandbox Code Playgroud)
我必须为此应用程序使用原始指针,我想知道如何nullptr在构造函数中设置所有 26 个指针?我是否必须循环next并单独设置每个人,还是有另一种更快的方法来做到这一点?
不知道为什么这个问题对我来说太难了.迭代地,这是蛋糕,但是一旦堆叠展开,它就会破坏我的整个功能.
它正确地找到针并且如果找到它则给函数赋值true.但是,一旦调用堆栈展开,它就会一直恢复为false.有谁知道如何解决这个或我的代码我做错了什么?
这是我到目前为止所拥有的......
bool mySubStr(char * needle, char * haystack)
{
int needleLength = strlen(needle);
int haystackLength = strlen(haystack);
bool found = false;
if(needleLength < haystackLength)
{
if(strncmp(haystack, needle, needleLength) == 0)
{
found = true;
}
else
{
mySubStr(needle, haystack + 1);
}
}
return found;
}
Run Code Online (Sandbox Code Playgroud) 我很难理解这部分意味着什么:
if (!s || !*s) //What is this?!?!
{
printf("\n");
return;
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要功能:
#include <conio.h>
#include <stdio.h>
#include <string.h>
void func1(char *s);
int main()
{
func1("SABABA");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而我的func1:
void func1(char *s)
{
int a, b, c;
if (!s || !*s)
{
printf("\n");
return;
}
b = strlen(s);
while (b>2)
{
a = 0;
if (s[a] == s[b - 1])
{
for (c = b - 1; c >= a && s[a] == s[c]; a++, c--);
if (c<a)
{ …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++17 ×2
constructor ×2
arrays ×1
boringssl ×1
c ×1
c++14 ×1
clang ×1
definition ×1
gcc ×1
iterator ×1
pointers ×1
qualifiers ×1
stdvector ×1
string ×1
templates ×1
type-traits ×1
unique-ptr ×1
vector ×1