按照这里写的文章:
我遇到了这个代码(为了清楚起见,缩短并更改了):
template <class T> struct hasSerialize
{
// This helper struct permits us to check that serialize is truly a method.
// The second argument must be of the type of the first.
// For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works!
// reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail!
// Note: It only works with integral constants and pointers (so function pointers work).
// In our case …Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming template-meta-programming type-deduction
基本上,我想让一个函数对于向量(类型)参数和非向量类型参数表现不同。
#include <vector>
using namespace std;
template <typename type>
struct is_vector {
static const bool value = false;
};
template <typename type>
struct is_vector<vector<type>>
{
static const bool value = true;
};
template <typename type>
type read()
{
if (is_vector<type>::value)
{
type vec(10);
vec.front()=1;//left of '.front' must have class/struct/union
return vec;
}
else
{
return{};
}
}
int main()
{
auto i= read<int>();
}
Run Code Online (Sandbox Code Playgroud)
我想在使用 vector 作为类型名时返回一个向量,在使用 int 作为类型名时返回一个 int 。
但既然 is_vector(int)::value 返回 false ,为什么我的编译器会报告“‘.front’的左边必须有类/结构/联合”?我怎样才能让它工作?
我想要实现的是将字符串正确反序列化为向量(类型)或向量(向量(类型))。
我需要递归调用 read 函数,同时传递一个多重向量作为模板参数,但编译器禁止我这样做。 …
C++14 草案 n4140 读取
T应为枚举类型
为template <class T> struct underlying_type。
写作有多糟糕
std::conditional_t<std::is_enum<T>::value, std::underlying_type_t<T>, foo>
Run Code Online (Sandbox Code Playgroud)
什么时候T可以是任意类型?我会进入 UB 吗,编译器会删除我的 $HOME (因为语言律师说“在 UB 下任何事情都可能发生”)?
c++ metaprogramming operator-precedence template-meta-programming evaluation-strategy
如何写一个模板 meta_set<class...Args>,使得 meta_set::type` 对于 Ts 的所有排列都是相同的?
换句话说,meta_set<>::type无论顺序如何,只要参数列表相同,我们就希望具有相同的值,也就是说,当将其视为一个集合(或多集合,如果更容易的话)。
例如,
std::is_same< meta_set<int,double,string>::type, meta_set<double,string,int>::type >::value == true
std::is_same< meta_set<int,double,string>::type, meta_set<double,string,bool>::type >::value == false
Run Code Online (Sandbox Code Playgroud)
当您希望每个模板参数集有一个模板的单个实例化时,这可能会派上用场。
注意:这不是家庭作业,而是我在工作中处理模板繁重代码时感到好奇的事情。我不是元编程专家,所以我想也许人们可以分享他们的知识。
c++ template-meta-programming variadic-templates c++11 c++14
我正在尝试通过实现一些功能来学习 C++ 模板元编程。我知道已经在 stackoverflow 上提供了这个特定问题的解决方案,我感兴趣的是了解为什么这个解决方案不起作用。这是代码:
template < std::size_t... Ns , typename... Ts >
auto tail_impl( std::index_sequence<Ns...> , std::tuple<Ts...> t )
{
return std::make_tuple( std::get<Ns+1u>(t)... );
}
template <class F, class... R >
tuple<R...> tail( std::tuple<F,R...> t )
{
return tail_impl( std::make_index_sequence<sizeof...(R)>() , t );
}
template<class X, class F, class... R>
constexpr bool check_for_type(tuple<F,R...> t) {
if constexpr(is_same<F,X>::value) {
return true;
}
return check_for_type<X>(tail(t));
}
template<class X>
constexpr bool check_for_type(tuple<> t) {
return false;
}
int main( int …Run Code Online (Sandbox Code Playgroud) 是否可以使用以下用法constexpr计算std::tuple元素类型的大小总和的函数:
static_assert(sum_size(std::tuple<int, bool>) == 5, "not 5!");
Run Code Online (Sandbox Code Playgroud)
?
这不直接回答我的问题,因为DoSomething不是constexpr功能.我需要在编译时调用DoSomething.或者也许有人能解释如何使用boost::fusion::for_each与static_assert()?
c++ templates template-meta-programming variadic-templates c++14
给定 a ,std::tuple<A, B, ...> fooC++14 中是否有任何通用(模板化)函数或技术来获取包含std::tuple<B, ...> bar除第一个元素之外的所有元素的新元组foo?或者,也许是 Boost 中的某些东西?
我已经使用参数包和一些模板元编程编写了一个辅助函数来执行此操作,但我很想扔掉所有这些东西!
这就是我目前正在做的事情。我定义了一个辅助函数unshift_tuple(),它返回一个元组,其中包含传递给函数的元组中除第一个元素之外的所有元素。的实现unshift_tuple()使用一个辅助函数unshift_tuple_with_indices(),该函数采用包含要提取的元组索引的参数包;帮助sequential_integer_list器类型用于使用模板元编程生成适当的索引列表参数包。丑陋的!
#include <tuple>
template <size_t... Integers>
struct integer_list {};
template <size_t N, size_t... Args>
struct sequential_integer_list : sequential_integer_list<N - 1, N - 1, Args...> {};
template <size_t... Args>
struct sequential_integer_list<0, Args...> { typedef integer_list<Args...> type; };
template <typename FirstElement, typename... Elements, size_t... Indices>
static std::tuple<Elements...> unshift_tuple_with_indices(
const std::tuple<FirstElement, Elements...>& tuple,
integer_list<Indices...> index_type)
{
return …Run Code Online (Sandbox Code Playgroud) 我有一个变种 ScalarVar
using ScalarVar = std::variant<int, std::string>;
Run Code Online (Sandbox Code Playgroud)
和一个变体Var,它本身可能是一个ScalarVar或一个std::vector的ScalarVar小号
using Var = std::variant<ScalarVar, std::vector<ScalarVar>>;
Run Code Online (Sandbox Code Playgroud)
我想打一个功能template<typename T, typename Variant> T Get(const Variant& var);是,当给定一个变种,不包括内部变形就只能像std::get<T>,也就是说,它会返回的值T,如果Variant当前包含T,或者如果考虑到包含其他变种的变体,它会递归地获取包含键入直到找到非变体,然后返回。
到目前为止,这是我最好的尝试:
#include <iostream>
#include <variant>
#include <string>
#include <typeindex>
#include <vector>
#include <map>
template<typename T, typename... T2>
struct is_variant { static inline constexpr bool value = false; };
template<typename... T>
struct is_variant<std::variant<T...>> { static inline constexpr bool value = true; }; …Run Code Online (Sandbox Code Playgroud) c++ variant algebraic-data-types template-meta-programming c++17
我将如何制作std::vector包含a中包含的所有类型的默认构造实例的 a std::variant?
using TaskVariant = std::variant<TypeA, TypeB, TypeC>;
std::vector<TaskVariant> variants;
// I'd like to do this in a loop
variants.push_back(TypeA());
variants.push_back(TypeB());
variants.push_back(TypeC());
Run Code Online (Sandbox Code Playgroud) 使用类型特征,我可以执行以下操作:
template<typename Rect> Rect& move(Rect& rc, size_type<Rect> delta)
{
rc.left += delta.width;
rc.right += delta.width;
rc.top += delta.height;
rc.bottom += delta.height;
return rc;
}
template<typename Rect> Rect& move(Rect& rc, point_type<Rect> to)
{
int w = w(rc);
int h = h(rc);
rc.left = to.x;
rc.top = to.y;
rc.right = rc.left + w;
rc.bottom = rc.top + h;
return rc;
}
Run Code Online (Sandbox Code Playgroud)
但是如何在不更改函数名称的情况下允许传递任何大小和点类型?显然我不能这样做:
template<typename Rect, typename Size> Rect& move(Rect& rc, Size delta);
template<typename Rect, typename Point> Rect& move(Rect& rc, Point to); …Run Code Online (Sandbox Code Playgroud) c++ templates overloading metaprogramming template-meta-programming