是否可以声明函数bar具有与函数相同的签名foo?
int foo(int a)
{
return 0;
}
decltype(foo) bar
{
return 1;
} //imaginary syntax
Run Code Online (Sandbox Code Playgroud) 考虑以下定义.
char right_string[]="::right_one.";
char wrong_string[]="::wrong_one.";
template<const char* str>
void f(){
static_assert(str==::right_string, "Pass me ::right_string!");
}
struct Test{
static constexpr char right_string[]="template_struct::right_one";
static constexpr char wrong_string[]="template_struct::wrong_one";
template<const char* str>
static void f(){
static_assert(str==right_string, "Pass me template_struct::right_string!");
}
};
int main(){
f< ::right_string>(); //compiles, as expected
f< ::wrong_string>(); //does not compile, as expected
Test::f<Test::right_string>(); //compiles, as expected
Test::f<Test::wrong_string>(); //error in Test::f: non-constant condition for static assertion
}
Run Code Online (Sandbox Code Playgroud)
完整的错误是
../main.cpp:16:3:错误:静态断言的非常量条件
../main.cpp:16:3:错误:'(((const char*)(&Test :: wrong_string))==((const char*)(&Test :: right_string)))'不是不断表达
我认为,这是一个编译器错误,因为它没有了意义constexpr表达的内内斯static_assert按我通过作为模板参数(是否修改Test::right_string …
void f(int count, ...){
//whatever
}
struct somestruct{
size_t a, b, c;
};
int main() {
somestruct s;
f(1, s); //what is actually passed?
}
Run Code Online (Sandbox Code Playgroud)
是整个struct复制并传递到堆栈?如果是这样,复制构造函数被称为?指针是否通过?这样安全吗?
我正在尝试寻找一个看起来像内存泄漏的服务器软件中的一个非常难以回避的错误,但memcheck根本没有帮助.我的猜测是,实例化并且从未删除的内存确实没有泄露,所以有一个引用,但现在对程序没用,应该删除.是否有一个工具可以计算内存中的访问而不是引用,因此评估堆中对象的有效使用情况?
这只是一个关于样式的问题:我不喜欢C++模板元编程的方式,它要求你使用返回类型或为SFINAE的技巧添加额外的伪参数.所以,我提出的想法是将SFINAE事物放在模板参数定义本身中,如下所示:
#include <iostream>
#include <boost/type_traits/is_array.hpp>
#include <boost/utility/enable_if.hpp>
using namespace std;
template <typename T, typename B=typename boost::enable_if< boost::is_array<T> >::type > void asd(){
cout<<"This is for arrays"<<endl;
}
template <typename T, typename B=typename boost::disable_if< boost::is_array<T> >::type > void asd(){
cout<<"This is for NON arrays"<<endl;
}
int main() {
asd<int>();
asd<int[]>();
}
Run Code Online (Sandbox Code Playgroud)
这个例子让g ++抱怨:
../src/afg.cpp:10:97:错误:重新定义'template void asd()'
SFINAE就在那里工作,因为如果我删除例如一个disable_if,编译错误是:
../src/afg.cpp:15:12:错误:没有匹配函数来调用'asd()'
这就是我想要的.
那么,有没有办法在一个函数的"正常"签名中完成SFINAE,即返回类型+参数列表?
编辑:这最终我将在真正的代码中尝试:
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T, typename enable_if< is_array<T>::value, int >::type =0 > void asd(){ …Run Code Online (Sandbox Code Playgroud) shm_open()mmap() 预定义的大 lengthfork() (几次)ftruncate() 随意这样做的目的是确保生成的每个进程fork()在同一地址都有一个共享段.然而,我不想让RAM一直保持忙碌,而是动态调整它的大小(大小为0 - 大length).
这可以吗?有UB吗?
考虑以下代码:
#include <iostream>
#include <type_traits>
using namespace std;
template<typename T_orig> void f(T_orig& a){
a=5;
}
template<typename T_orig, typename T=T_orig&> void g(T a){
a=8;
}
int main() {
int b=3;
f<decltype(b)>(b);
cout<<b<<endl;
g<decltype(b)>(b);
cout<<b<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这打印
5
5
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么第二个版本&会丢失吗?
有没有办法使lambda衰减到指针,而没有明确地转换为正确的签名?这将整理一些代码:
template<typename T> T call(T(*func)()){ return func(); }
int ptr(){ return 0; }
int main(){
auto ret1 = call(ptr);
auto ret2 = call((int(*)())([]{ return 0; }));
auto ret3 = call([]{ return 0; }); //won't compile
}
Run Code Online (Sandbox Code Playgroud)
很明显,call只有在lambda衰减到指针时调用才能工作,但我猜测只有在选择了正确的函数重载/模板之后才会发生这种情况.不幸的是,我只能想到涉及模板的解决方案来制作一个带有任何签名衰变的lambda ,所以我回到原点.
我有这个班轮:
perl -pe 's|.*?((\d{1,3}\.){3})xxx.*|\1|'
Run Code Online (Sandbox Code Playgroud)
我用一些输入来提供这个命令,就像192.168.1.xxx它一样.现在,我想在输出序列中附加一个0,但是当然如果我只是在\1它被解析为第十个捕获组之后追加0 .我怎样才能将它与\1指令连接起来?
我有一个函数,它需要几个布尔模板参数:
template<bool par1, bool par2, bool par2>
void function(int arg1, int arg2, int arg3);
Run Code Online (Sandbox Code Playgroud)
我想在编译时自动生成(使用任何模板魔法,如果需要,使用C++ 11)一个表(或类似于C++元编程的有趣结构中的某些东西)的函数指针到所有值的组合模板参数par*,这样我就可以构造一个函数,它将这些模板参数作为运行时参数并转发到正确的模板实例化:
void runtime_function(bool par1, bool par2, bool par3, int arg1, int arg2, int arg3);
Run Code Online (Sandbox Code Playgroud)
我认为,如果不是模板函数,可以通过模板模板参数来实现这一点:
template<template<bool> class T> class CombinationsOfTemplateParameters;
template<template<bool, bool> class T> class CombinationsOfTemplateParameters;
template<template<bool, bool, bool> class T> class CombinationsOfTemplateParameters;
//and so on, up to some implementation defined hard limit.
Run Code Online (Sandbox Code Playgroud)
但据我所知,没有办法指向通用模板函数,不保留其模板参数.因此,我首先不知道如何将其传递给模板参数列表中的某个辅助类.
有没有办法解决这个问题?
c++ templates metaprogramming template-meta-programming c++11