问题很简单(要问),与记忆std::bitset<32>是一回事uint32_t吗?或者更像是std::array<bool, 32>?
我经常这样做:
uint32_t index : 20;
uint32_t magic : 12;
Run Code Online (Sandbox Code Playgroud)
那么它和这段代码一样吗?
std::bitset<20> index;
std::bitset<12> magic;
Run Code Online (Sandbox Code Playgroud) 我认为这是不可能的,但我想在放弃之前问你.
我想要像constexpr增量一样的东西.
#include <iostream>
constexpr int inc() {
static int inc = 0;
return inc++;
}
class Foo {
static const int Type = inc();
};
class Foo2 {
static const int Type = inc();
};
int main() {
std::cout << "Foo1 " << Foo1::Type << st::endl;
std::cout << "Foo2 " << Foo2::Type << st::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想把它称为某些类而不是手动(我使用CRTP),为每个类提供不同的类型,但类型需要是const.无论如何在C++中实现类似的东西?(C++ 17 + TS)
我正在尝试使用嵌套的constexpr lambdas创建一个curried接口,但编译器不认为它是一个常量表达式.
namespace hana = boost::hana;
using namespace hana::literals;
struct C1 {};
template < typename T,
std::size_t size >
struct Array {};
constexpr auto array_ = [] (auto size) {
return [=] (auto type) {
return hana::type_c<Array<typename decltype(type)::type, size()>>;
};
};
int main() {
constexpr auto c1 = hana::type_c<C1>;
constexpr auto test = hana::type_c<Array<typename decltype(c1)::type, hana::size_c<100>()>>;
constexpr auto test2 = array_(hana::size_c<100>)(c1);
}
Run Code Online (Sandbox Code Playgroud)
我之前发布了一个问题,因为我找到了一个不同的最小例子,但这还不够.
错误:
test2.cpp: In instantiation of ‘<lambda(auto:1)>::<lambda(auto:2)> [with auto:2 = boost::hana::type_impl<C1>::_; auto:1 = boost::hana::integral_constant<long unsigned int, 100>]’:
test2.cpp:31:54: …Run Code Online (Sandbox Code Playgroud) 问题很简单,我怎么能产生:
std::tuple<float, int, double>
Run Code Online (Sandbox Code Playgroud)
如果我知道类型:
struct Foo { float a; int b; double c; };
Run Code Online (Sandbox Code Playgroud)
我怎样才能在转换中检索数据?
我开始学习 C#,我通常使用 C++。我正在尝试适应很多事情,但std::array似乎不可能......
我只想运行这种代码:
public struct Foo {};
public struct Test
{
public Foo value[20];
};
Run Code Online (Sandbox Code Playgroud)
我不想每次使用这个结构时都分配,我也不想使用一个类......我看到了fixed关键字,但它只适用于基本类型......没有像std::array?这样简单的东西吗?我什至可以在 C 中做到这一点。
你会如何解决这个问题?(即使它仍然是动态分配的..)
我正在尝试使用模板类(此处Foo),其基本类型如下:
hana::tuple<hana::pair<hana::type<int>, Runtime>>与Runtime一个不可思议的课程constepxr.但是类型可以用几种方式构造,这就是我使用的原因:
hana::tuple<hana::pair<hana::type<int>, hana::type<Runtime>>> 在编译时完成工作.所以问题基本上是如何从第一个元组转换为第二个类型.我想知道是否有什么hana可以帮助我.或者甚至更好,关于那种"转换"的一些提示.
namespace hana = boost::hana;
using namespace hana::literals;
struct Runtime { std::vector<int> data; };
template < typename T >
struct Foo {
T data;
};
constexpr decltype(auto) convertMap(auto storageMap) {
return hana::make_type(hana::transform(
storageMap,
[] (auto pair) {
return hana::make_pair(
hana::first(pair),
typename decltype(hana::typeid_(hana::second(pair)))::type {});
}));
}
int main() {
constexpr auto map = hana::make_tuple(
hana::make_pair(hana::type_c<int>, hana::type_c<Runtime>)
);
constexpr auto result = convertMap(map);
static_assert(result == …Run Code Online (Sandbox Code Playgroud) 可能只是概念很糟糕,但我不明白为什么.并没有找到任何构造函数的例子.或者它可能与构造函数无关......
template < typename T >
concept bool C_Object() {
return requires {
T();
};
}
template < C_Object Object>
class DefaultManager {
// Content;
};
template < C_Object Derived >
class Component {
// Content
};
struct Test : public Component<Test> {
int data;
Test() = default;
};
int main() {
Test test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
test2.cpp:21:36: error: template constraint failure
struct Test : public Component<Test> {
^
test2.cpp:21:36: note: constraints not satisfied
test2.cpp:2:14: note: within ‘template<class T> …Run Code Online (Sandbox Code Playgroud) 我正在尝试hana::type使用一对来访问hana::second...
namespace hana = boost::hana;
using namespace hana::literals;
struct Key {};
struct Foo {};
int main() {
auto test = hana::make_tuple(
hana::make_pair(
hana::type_c<Key>,
hana::type_c<Foo>));
typename decltype(hana::type_c<Foo>)::type finalTest; //Ok
typename decltype(hana::second(test[0_c]))::type finalTest2; //Error
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下编译器错误:
stacktest.cpp: In function ‘int main()’:
stacktest.cpp:17:12: error: decltype evaluates to ‘boost::hana::type_impl<Foo>::_&’, which is not a class or enumeration type
typename decltype(hana::second(test[0_c]))::type finalTest2;
Run Code Online (Sandbox Code Playgroud)
为什么不按预期hana::second返回包含的结果hana::type?
我刚刚遇到了 gcc 和 clang 的编译错误,所以我认为这段代码是不可能的:
template < typename T >
struct Type {
using type = T;
};
template < int size = 1024 >
struct Foo {};
constexpr auto test_ = [] (const int size) {
return Type<Foo<size>>;
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
test.cpp:12:19: error: non-type template argument is not a constant expression
return Type<Foo<size>>;
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
问题是为什么?size是一个常量值,应该能够适合作为模板参数不是吗?我已经使用了一些静态常量值作为模板参数,但似乎不支持这种情况。
我正在逐步学习模板,我认为这是我当前项目需要做的最后一件事:
template < typename Object, bool Shared>
class Foo {
private:
struct SubFoo {
//if Shared == true -> FooShared foo;
//if Shared == false -> FooBasic foo;
??? foo;
Object data;
};
};
Run Code Online (Sandbox Code Playgroud)
我知道MagicType foo;这样做不会那么容易,不用担心修改代码.我没有C++限制(g ++ 6.2,我想知道如何获得g ++ 7)所以即使是概念也是受欢迎的.