当第一个模板参数不是 POD 类型时,我想部分专门化一个类。这是我想出的:
#include <iostream>
#include <type_traits>
template <typename T, bool = std::is_pod<T>>
struct A
{
void print()
{
std::cout << "POD\n";
}
};
template <typename T>
struct A<T, false>
{
void print()
{
std::cout << "Non-POD\n";
}
};
int main()
{
A<int> a;
A<std::string> c;
a.print();
c.print();
}
Run Code Online (Sandbox Code Playgroud)
然而,这不会编译并产生三种类型的错误(对于字符串大小写重复):
<source>:4:43: error: expected primary-expression before '>' token
4 | template <typename T, bool = std::is_pod<T>>
| ^~
<source>: In function 'int main()':
<source>:24:10: error: template argument 2 is invalid …Run Code Online (Sandbox Code Playgroud) 这可能很简单,但我没有做对。我有一个“位掩码”枚举,它的值all指示所有位均已设置。但是,我无法让它使用 ~0 翻转所有位。出现以下错误:
<source>:11:16: error: enumerator value '-1' is outside the range of underlying type 'uint_fast8_t' {aka 'unsigned char'}
11 | all = ~0x0,
|
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为它实际上应该适合 uint8_t no?这是我的代码(godbolt):
#include <iostream>
int main()
{
enum mask_t : uint_fast8_t {
first = 0x1,
second = 0x2,
all = ~0x0,
} mask;
mask = all;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用自定义函数来删除 unique_ptr 。但编译器给了我错误。不知怎的,我怀疑这是因为我的删除器不是类类型。但我发誓它以前可以使用函数,我曾经插入 C 函数就很好了。什么是真正的交易?
#include <cstdio>
#include <memory>
#include <utility>
using cTYPE = struct {
int a;
};
void delete_wrapper(cTYPE* ptr)
{
return free(ptr);
}
cTYPE* new_wrapper()
{
return static_cast<cTYPE*>(malloc(sizeof(cTYPE)));
}
int main()
{
auto foo = []() {
using tmp_storage_type = std::unique_ptr<cTYPE, decltype(delete_wrapper)>;
static tmp_storage_type obj;
obj = tmp_storage_type{ new_wrapper(), delete_wrapper };
};
foo();
}
Run Code Online (Sandbox Code Playgroud)
错误(其中,clang 在这里给出了更好的错误):
<source>:23:33: note: in instantiation of template class 'std::unique_ptr<cTYPE, void (cTYPE *)>' requested here
static tmp_storage_type obj;
^
<source>:24:15: error: no …Run Code Online (Sandbox Code Playgroud) 我正在尝试默认初始化一个配置结构,该结构由一些字段组成,其中包括另一个从属配置结构 - 使用宏:
#include <cstdio>
#define MYCLASS_DEFAULT_CONFIG mylib::options { \
.a_ = 2, \
.b_ = 3, \
.subopts_ = MYCLASS_DEFAULT_SUBOPT_CONFIG() \
}
#define MYCLASS_DEFAULT_SUBOPT_CONFIG mylib::sub_options { \
.c_ = 'A'; \
.d_ = 'H'; \
}
namespace mylib
{
struct sub_options
{
char c_;
char d_;
};
struct options
{
int a_;
int b_;
sub_options subopts_;
};
class myclass
{
myclass(options opts)
: opts_ { opts }
{
}
options opts_;
};
}
int main()
{
mylib::myclass some_class(MYCLASS_DEFAULT_CONFIG()); …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我尝试获取派生自 的自定义类型的元组大小std::tuple。但编译器抱怨这std::tuple_size是不完整的......我无法真正理解,因为struct foo此时已经明确定义了。自然也应该如此type_descriptor<foo>。这个错误是从哪里来的?
#include <utility>
#include <tuple>
#include <cstdio>
struct foo
{
int a_;
int b_;
};
template <typename T>
struct type_descriptor
{};
template<>
struct type_descriptor<foo>
: std::tuple<int, bool>
{
};
int main(){
printf("Size of tuple = %zu\n", std::tuple_size_v<type_descriptor<foo>>);
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
<source>:89:27: required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/utility.h:75:61: error: incomplete type 'std::tuple_size<type_declarator<foo> >' used in nested name specifier
75 | inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
|
Run Code Online (Sandbox Code Playgroud) c++ ×5
c++20 ×2
aggregate ×1
bitmask ×1
c++17 ×1
constructor ×1
enums ×1
stdtuple ×1
struct ×1
unique-ptr ×1