我有一个vec_base像这样定义的类:
template<typename T, std::size_t Size>
class vec_base;
Run Code Online (Sandbox Code Playgroud)
我想把它专门化
vec_base<float, /* any multiple of 4 */>
Run Code Online (Sandbox Code Playgroud)
和
vec_base<double, /* any multiple of 2 */>
Run Code Online (Sandbox Code Playgroud)
例如,可以独立地拥有特定成员
vec_base<int, 6>
Run Code Online (Sandbox Code Playgroud)
哪个会有我已定义的通用成员
我很难实现这个,因为允许的宽松大小(4或2的任意倍数),如果它特别是2或4我知道我可以执行完全专业化,但事实并非如此:/
我该怎么做?感谢任何帮助,我总是喜欢学习新的语言技巧!
好吧所以我到目前为止:
template<std::size_t Size>
struct is_div_by_4{static const bool value = (Size % 4 == 0);};
// have to define because of template requirements
// about not being dependent on other parameters
template<typename T, std::size_t Size, bool is_special>
class vec_base;
template<typename T, std::size_t Size>
class vec_base<T, Size, false>{
// generic …Run Code Online (Sandbox Code Playgroud) 我遇到了需要一个具有相同输出的模板函数的问题,只要它的模板参数是相同的,与位置无关.鉴于总有两个参数.
我有一个功能:
template<typename Lhs, typename Rhs>
int func();
Run Code Online (Sandbox Code Playgroud)
我想func<int, float>()和func<float, int>()调用相同的代码.
我想过一个宏,我想避免使用它,但是当两种类型相同时我不需要重复代码.所以宏如:
#define DEF_TEMPL_IMPL(lhs, rhs, ret) \
template<>\
auto func<lhs, rhs>(){ return ret; }\
template<>\
auto func<rhs, lhs>(){ return func<lhs, rhs>(); }
Run Code Online (Sandbox Code Playgroud)
将无法编译因为DEF_TEMPL_IMPL(float, float, 3)会导致重新定义func<>
我认为SFINAE就是这里的答案,但我们无法想到解决方案.
我将继续思考这一点,但是在回答这个问题之前,堆栈溢出的一些优秀思想可能会比我能提出的更好或更优雅的解决方案.
那怎么能实现呢?
我尝试了许多功能,例如ShowWindow&IsWindowVisible至少尝试在窗口最小化时给出结果,更不用说恢复它了。无论窗口是否最小化,这些函数都会不断返回 false。我一直在使用也试图GetWindowPlacement与SetWindowPlacement没有成功。我HWND发现 ChromeFindWindow(TEXT("Chrome_WidgetWin_1"), NULL);成功了,但我想测试/恢复窗口是否最小化并且过去 10 个小时没有任何显示。
我有一个我正在编写的类,它将为其中一个构造函数采用特殊类型,它可以是符合我要求的任何类型.我遇到了这个模板化构造函数导致我的副本并将构造函数移动为非法重载的问题!
我的班级是这样的:
template<typename ... Types>
class myclass{
public:
myclass(const myclass &other){/* copy constructor */}
myclass(myclass &&other){/* move constructor */}
template<typename Special>
myclass(Special &&arg){/* stops copy/move implementations */}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个限制?
如果我有一个现有的非平凡变量,并且我想用新的内容重新分配它,我将在与赋值相同的行上声明,我应该使用移动语义吗?
我的问题来自以下场景:
std::vector<string> existing = { ... };
int main(int argc, char *argv[]){
const char *bunch_of_strings = ... ;
std::stringstream ss(bunch_of_string);
existing = std::move(std::vector<std::string>(std::istream_iterator<std::string>(ss), {}));
}
Run Code Online (Sandbox Code Playgroud)
我应该这样做,如果我没有,编译器是否会同样优化它,或者它最好不要?
假设我有一个功能func:
template<typename T>
auto func(T arg){
std::cout << std::boolalpha;
std::cout << "T is ref: " << std::is_reference<T>::value << '\n';
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以强制T将其推导为引用类型,而无需显式指定模板参数?
就像能够写出这样的东西:
auto main() -> int{
auto x = 5;
func(std::ref(x));
}
Run Code Online (Sandbox Code Playgroud)
但不必专门从事std::reference_wrapper.
static_casting 并不能阻止 的衰变int&为intfor T。
想象一下我无法更改函数签名。
我目前有一个数据库类,其中包含一些包含数据库变量的无序映射.我编写了添加和删除变量的函数,但现在想要一种方法来轻松访问和修改它们.
我的功能是这样的:
template<typename T>
T &get(const std::string &name){
if(typeid(T) == typeid(int))
return this->ints[name];
else if(typeid(T) == typeid(float))
return this->floats[name];
... ect ...
}
Run Code Online (Sandbox Code Playgroud)
如果给出了无效类型,则抛出错误.
ints是类型std::unordered_map<std::string, int>
并且floats同样定义.
对我来说,这看起来是正确的.但是当我尝试运行它时,我收到以下错误:
database.hpp:98: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Run Code Online (Sandbox Code Playgroud)
看起来无序地图没有返回引用,问题是什么?