我可以使用C++类型特征来检查类型是否是类似STL的容器吗?我已经知道GCC的内置功能,__is_class但如果可能的话,我想更具体一些.
我有一个问题,我需要检测给定类型是否是已知嵌套类型的实例,如std::vector::iterator在编译时.我想创建类型特征is_std_vector_iterator:
#include <type_traits>
#include <vector>
template<typename T> struct is_std_vector_iterator : std::false_type {};
template<typename T, typename Allocator>
struct is_std_vector_iterator<typename std::vector<T,Allocator>::iterator>
: std::true_type
{};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我收到编译器错误:
$ g++ -std=c++0x test.cpp
test.cpp:7: error: template parameters not used in partial specialization:
test.cpp:7: error: ‘T’
test.cpp:7: error: ‘Allocator’
Run Code Online (Sandbox Code Playgroud)
是否可以检查依赖类型std::vector<T,Allocator>::iterator?
这是一个激发这种特质的用例:
template<typename Iterator>
Iterator my_copy(Iterator first, Iterator last, Iterator result, std::true_type)
{
// iterators are just pointer wrappers; call memcpy
memcpy(&*result, &*first, sizeof(typename Iterator::value_type) * …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我有一个std::future<some_type>调用API A,但需要提供API B与std::future<void>:
std::future<some_type> api_a();
void api_b(std::future<void>& depend_on_this_event);
Run Code Online (Sandbox Code Playgroud)
如果没有提议的功能,例如.then()或when_all(),是否有任何有效的方法可以抛弃附加到a的值std::future<T>,只留下std::future<void>表示事件完成的底层?
像下面这样的东西可以工作,但可能效率低下:
auto f = api_a();
f.wait();
auto void_f = std::async(std::launch::defer, []{});
api_b(void_f);
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我想提供一个函数的泛型版本,foo只有在调用绝对没有其他匹配时才可以应用.我如何修改下面的代码,这样last_resort::foo是更差的比赛derived::type比base::foo?我想找到一个解决方案,它不涉及修改定义, bar而是保留参数的类型last_resort::foo.
#include <iostream>
namespace last_resort
{
template<typename T> void foo(T)
{
std::cout << "last_resort::foo" << std::endl;
}
}
template<typename T> void bar(T)
{
using last_resort::foo;
foo(T());
}
namespace unrelated
{
struct type {};
}
namespace base
{
struct type {};
void foo(type)
{
std::cout << "base::foo" << std::endl;
}
}
namespace derived
{
struct type : base::type {};
}
int main()
{
bar(unrelated::type()); // calls last_resort::foo
bar(base::type()); // …Run Code Online (Sandbox Code Playgroud) 假设我有一个my_struct包含成员变量的类型f,它是一个函数.它可能f是一个c ++ 11 lambda函数.
因为分配给lambda对象是非法的,所以我想以my_struct这样一种方式实现赋值运算符:when f是lambda时,它不被赋值.
是否有可能构建一个is_lambda可以检查lambda-ness类型的类型特征?
在代码中:
#include <type_traits>
template<typename Function> struct is_lambda
{
// what goes here?
};
template<typename Function> struct my_struct
{
Function f;
my_struct &do_assign(const my_struct &other, std::true_type)
{
// don't assign to f
return *this;
}
my_struct &do_assign(const my_struct &other, std::false_type)
{
// do assign to f
f = other.f;
return *this;
}
my_struct &operator=(const my_struct &other)
{
return do_assign(other, typename is_lambda<Function>::type());
}
};
Run Code Online (Sandbox Code Playgroud) 我想编写一个函数模板,apply它接收一些函数f,一个整数i和一个参数包.apply需要解压缩参数并应用于f它们,除了ith参数,pi.因为pi,它需要g在将其作为参数传递之前调用其他函数f.
看来我需要一种方法将参数包分为左侧,第ith个参数和右侧.这可能吗?在代码中:
template<int i, typename Function, typename... Parms>
void apply(Function f, Parms... parms)
{
auto lhs = // what goes here?
auto pi = // what goes here?
auto rhs = // what goes here?
f(lhs..., g(pi), rhs...);
}
Run Code Online (Sandbox Code Playgroud) 我试图用来thrust::transform减少a的每个元素的常量值device_vector.如您所见,最后一行不完整.我试图从所有元素减去常数,fLowestVal但不知道究竟是多少.
thrust::device_ptr<float> pWrapper(p);
thrust::device_vector<float> dVector(pWrapper, pWrapper + MAXX * MAXY);
float fLowestVal = *thrust::min_element(dVector.begin(), dVector.end(),thrust::minimum<float>());
// XXX What goes here?
thrust::transform(...);
Run Code Online (Sandbox Code Playgroud)
另一个问题:一旦我对其进行了更改device_vector,更改是否也适用于p阵列?
谢谢!
我有一个应用程序,我正在构建一个函数,marshal_and_apply它f使用一些参数调用其他函数(或函子).marshal_and_apply的工作是根据参数的类型对参数应用一些特殊的编组f.
如果一个f的参数是一种特殊类型的,marshal_me<T>,然后marshal_and_apply将它传递给元帅之前通过一些特殊分配的存储参数f.为了执行分配,必须知道所有参数的存储要求,marshal_and_apply然后才能对其进行编组.
一些例子:
template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args... args);
void func1(int x, int y);
void func2(marshal_me<int> x, int y);
void func3(marshal_me<int> x, marshal_me<int> y, marshal_me<int> z);
// this call would be equivalent to:
// func1(7,13)
marshal_and_apply(func1, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int));
// auto x = marshal_me<int>(7, storage);
// func2(x, 13); …Run Code Online (Sandbox Code Playgroud) ISO C++ 11规范的第5.1.2节第10节规定:
使用通常的非限定名称查找规则(3.4.1)查找捕获列表中的标识符; 每个这样的查找应该找到一个变量,其自动存储持续时间在本地lambda表达式的到达范围内声明.如果实体(即变量或此实体)出现在lambda表达式的捕获列表中,则称其被明确捕获.
这似乎意味着lambda无法捕获文件范围变量.例如,该程序应该是非法的:
#include <iostream>
int x = 13;
int main()
{
auto l = [](){ return x; };
std::cout << l() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,g++4.7.1会产生我期望的结果:
$ g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++11 lambda.cpp
$ ./a.out
13
Run Code Online (Sandbox Code Playgroud)
但clang …
c++ ×8
c++11 ×4
templates ×4
cuda ×2
lambda ×2
asynchronous ×1
future ×1
thrust ×1
type-traits ×1