我想有一个模板类(如float/double类型),但我使用Nvidia的CUDA和Optix公司和有多个其他类型(例如float2,double2,float3,...)依赖于所选择的模板类型。
像这样的东西:
#include <optixu/optixu_vector_types.h>
#include <type_traits>
template <class T>
class MyClass
{
MyClass()
{
if (std::is_same<T, float>::value)
{
typedef optix::float2 T2;
}
else if (std::is_same<T, double>::value)
{
typedef optix::double2 T2;
}
T2 my_T2_variable;
}
void SomeFunction()
{
T2 another_T2_variable;
};
};
Run Code Online (Sandbox Code Playgroud)
我现在的解决方案是有多个模板参数MyClass<T,T2,T3> my_object;,但这似乎有太多的开销和混乱。有没有办法使用上述所需的单个模板参数实现相同的目标?
我们可以通过流行的“ 删除-删除”惯用语从容器中删除一个元素/条目。但是,在应用此惯用语时,我们许多人会遇到一些问题:
人们可以很容易陷入的陷阱错别字像
c.erase(std::remove_if(c.begin(), c.end(), pred));
// , c.end() //---> missing here
Run Code Online (Sandbox Code Playgroud)
要么
c.erase((std::remove_if(c.begin(), c.end(), pred), c.end()))
// ^^ ^^
// extra () makes it pass only c.end() to the c.erase
Run Code Online (Sandbox Code Playgroud)std::list不std::list::remove_if()
为习惯用语选择自己的成员
。std::remove_if 不适用于关联容器。在c ++ 17的范围内,我们是否有比python范围更广,更不易打错的std::erase-std::remove_if东西,或者c ++ 20中是否会有这样的工具?std::erase_if
以下两种在a末尾插入新元素的方法在性能上是否有任何区别std::vector:
std::vector<int> vec = { 1 };
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
Run Code Online (Sandbox Code Playgroud)
std::vector<int> vec = { 1 };
int arr[] = { 2,3,4,5 };
vec.insert(std::end(vec), std::begin(arr), std::end(arr));
Run Code Online (Sandbox Code Playgroud)
就我个人而言,我喜欢方法2,因为它很好且简洁,可以一次性插入数组中的所有新元素。但
首先,我不使用所有元素初始化向量的原因是,在我的程序中,我根据条件添加了其余元素。
我有一个像
template <size_t N>
class A
{
template <size_t N>
someFunctions() {};
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建类的实例并在 for 循环中为一组许多值调用其中的函数,例如
// in main()
int main()
{
for (int i = 1; i <= 100; i++)
{
const int N = i; // dont know how to do this
A<N> a;
a.functionCalls();
}
}
Run Code Online (Sandbox Code Playgroud)
这该怎么做?希望有一种方法可以做到这一点。
c++ templates for-loop compile-time-constant template-classes
我有一个模板类和一个成员函数print()来打印数据。
template<typename T>
class A
{
public:
T data;
void print(void)
{
std::cout << data << std::endl;
}
// other functions ...
};
Run Code Online (Sandbox Code Playgroud)
然后,我想打印标量数据或矢量数据,所以我给出了一个专门的定义并得到一个编译器错误。
template<typename T>
void A<std::vector<T>>::print(void) // template argument list error
{
for (const auto& d : data)
{
std::cout << d << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
问题:为什么这个成员函数特化会报错?为向量定义打印函数的正确方法是什么?
解决方案 1:我已经测试了以下定义。
template<typename T>
class A<std::vector<T>>
{
public:
std::vector<T> data;
void print(void) { // OK
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
这个有效,但我必须将其他成员函数复制到这个专门的类中。
编辑:
解决方案2 …
c++ templates member-functions template-specialization c++17
考虑以下代码:
std::unordered_map<int, std::string> data;
data[5] = foo();
Run Code Online (Sandbox Code Playgroud)
按照什么顺序data[5]和foo()处理?如果foo()抛出异常,是否创建了5项目data?
如果行为取决于C++的版本,那些版本有何不同?
How to overload a simple local lambda function?
SSE of original problem:
#include <iostream>
#include <map>
void read()
{
static std::string line;
std::getline(std::cin, line);
auto translate = [](int idx)
{
constexpr static int table[8]{ 7,6,5,4,3,2,1,0 };
return table[idx];
};
auto translate = [](char c)
{
std::map<char, int> table{ {'a', 0}, {'b', 1}, {'c', 2}, {'d', 3},
{'e', 4}, {'f', 5}, {'g', 6}, {'h', 7} };
return table[c];
};
int r = translate(static_cast<int>(line[0]));
int c = translate(static_cast<char>(line[1]));
std::cout << r << …Run Code Online (Sandbox Code Playgroud) 我有一个需要通过CAN协议发送的float变量.为此,必须在4个uint8_t变量中切割32位的浮点数.
我完全不知道该怎么做.我首先考虑将float转换为int但我在Internet上找到的使用强制转换或联合的一些答案似乎不起作用.
这是我正在尝试做的一个简单示例:
float f;
uint8_t ut1,ut2,ut3,ut4;
//8 first bits of f into ut1
//8 second bits of f in ut2
...
// Then I can send the uint8_t through CAN
...
Run Code Online (Sandbox Code Playgroud)
谢谢.
c++ casting type-conversion uint8t floating-point-conversion
在解释与同事对对象的移动操作时,我基本上说移动操作不应该在容器中抛出异常,因为如果移动操作失败,则无法可靠地恢复原始对象.考虑到这一点,我想知道这是不是正确的,并且如果一个移动操作确实抛出,它可以将原始对象恢复到它的原始状态.
这样做的原因是,如果一个对象可以抛出,那么它将不会因为复制或将包含的对象从旧地址移动到新地址而抛出,而是在无法获取资源时抛出.所以所有的原始信息都应该存在.如果是这种情况,那么编译器是否应该无法反转它为重建原始对象所做的操作?
操作可能是一种方式,比如移动一个整数,但在这种情况下它可能只是终止应用程序,也许如果开发人员想要避免单向操作可以使用交换方法.
这只能在默认移动运算符上实现,就像有任何其他逻辑一样,编译器可能很难进行反向部分变换.
我是否过于简化了事情?有没有我遗漏的东西,如果没有非投掷移动构造函数/运算符,容器不会移动对象?
是否有相同的版本std::find(first, last)但是std::map?即,是否有一个版本std::map的find方法可以搜索a中的元素map,但是只将搜索限制在指定的[first, last)范围内?理想情况下,解决方案应该是对数的大小[first, last).
从我所看到的,std::map::find它本身不支持这个功能(它总是搜索整个地图).