带有附加arg的C ++模板问题(C++ Template issue with additional arg)
我有一种情况(在嵌入式系统上),我想从另一个任务调用函数。 我发现这种方法可以这样做http://www.drdobbs.com/elegant-function-call-wrappers/184401385 。 但是我有更多的论据要做什么。 最好的情况我有一个解决方案,这是在模板中,但我不知道如何实现这一点。 无论如何,也可以创建两个镜像,如下所示(在一个小型的Visual Studio C ++测试中)。 除了给出编译时错误的DeferCall2调用外,一切都运行正常。 我在这里搞错了什么?
#include "stdafx.h" #include "stdlib.h" #include "stdio.h" class Functor { public: virtual ~Functor() {} virtual void operator()() = 0; }; template< class CalleePtr, class MemFunPtr, class Parm1 > class MemberFunctor1 : public Functor { public: MemberFunctor1 ( const CalleePtr & pCallee, const MemFunPtr & pFunction, Parm1 aParm1 ) : pCallee(pCallee), pFunction(pFunction), aParm1(aParm1) { } virtual void operator()() { if ((pCallee != NULL) && (pFunction != NULL)) { ((*pCallee).*pFunction)(aParm1); } } private: CalleePtr pCallee; MemFunPtr pFunction; Parm1 aParm1; }; template< class CalleePtr, class MemFunPtr, class Parm1, class Parm2 > class MemberFunctor2 : public Functor { public: MemberFunctor2 ( const CalleePtr & pCallee, const MemFunPtr & pFunction, Parm1 aParm1, Parm2 aParm2 ) : pCallee(pCallee), pFunction(pFunction), aParm1(aParm1), aParm2(aParm2) { } virtual void operator()() { if ((pCallee != NULL) && (pFunction != NULL)) { ((*pCallee).*pFunction)(aParm1, aParm2); } } private: CalleePtr pCallee; MemFunPtr pFunction; Parm1 aParm1; Parm2 aParm2; }; template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1 > inline Functor * DeferCall ( const CalleePtr & pCallee, Ret(Callee::*pFunction)(Type1), const Parm1 & rParm1 ) { return new MemberFunctor1< CalleePtr, Ret(Callee::*)(Type1), Parm1 > (pCallee, pFunction, rParm1); } template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1, class Parm2> Functor * DeferCall2 ( const CalleePtr & pCallee, Ret(Callee::*pFunction)(Type1), const Parm1 & rParm1, const Parm2 & rParm2 ) { return new MemberFunctor2< CalleePtr, Ret(Callee::*)(Type1), Parm1, Parm2 > (pCallee, pFunction, rParm1, rParm2); } class TestObj { public: void test1 (int a) { printf("test 1 a=%d\r\n", a); } void test2 (int a, int b) { printf("test 2 a=%d, b=%d\r\n", a, b); } void test (void) { Functor * pCall = DeferCall(this, &TestObj::test1, 3); Functor * pCall1 = new MemberFunctor1 < TestObj *, void (TestObj::*)(int), int >(this, &TestObj::test1, 6); Functor * pCall2 = new MemberFunctor2 < TestObj *, void (TestObj::*)(int, int), int, int >(this, &TestObj::test2, 4, 5); Functor * pCall3 = DeferCall2(this, &TestObj::test2, 3, 3); // Compile time error. if (pCall) (*pCall)(); if (pCall1) (*pCall1)(); if (pCall2) (*pCall2)(); if (pCall3) (*pCall3)(); delete pCall; delete pCall1; delete pCall2; delete pCall3; } }; int _tmain(int argc, _TCHAR* argv[]) { TestObj *obj = new TestObj(); obj->test(); delete obj; return 0; }
I have a situation (on an embedded system) where I want call functions from another task. I found this method to do so http://www.drdobbs.com/elegant-function-call-wrappers/184401385 . But I what to do so with more arguments. Best case I have a solution where this is in the template to, but I have no clue how to realise that. Anyway, it is also possible to create two temples as shown below (in a small visual studio C++ test). Everything function fine, except the DeferCall2 call that give a compile time error. What mistake I made here?
#include "stdafx.h" #include "stdlib.h" #include "stdio.h" class Functor { public: virtual ~Functor() {} virtual void operator()() = 0; }; template< class CalleePtr, class MemFunPtr, class Parm1 > class MemberFunctor1 : public Functor { public: MemberFunctor1 ( const CalleePtr & pCallee, const MemFunPtr & pFunction, Parm1 aParm1 ) : pCallee(pCallee), pFunction(pFunction), aParm1(aParm1) { } virtual void operator()() { if ((pCallee != NULL) && (pFunction != NULL)) { ((*pCallee).*pFunction)(aParm1); } } private: CalleePtr pCallee; MemFunPtr pFunction; Parm1 aParm1; }; template< class CalleePtr, class MemFunPtr, class Parm1, class Parm2 > class MemberFunctor2 : public Functor { public: MemberFunctor2 ( const CalleePtr & pCallee, const MemFunPtr & pFunction, Parm1 aParm1, Parm2 aParm2 ) : pCallee(pCallee), pFunction(pFunction), aParm1(aParm1), aParm2(aParm2) { } virtual void operator()() { if ((pCallee != NULL) && (pFunction != NULL)) { ((*pCallee).*pFunction)(aParm1, aParm2); } } private: CalleePtr pCallee; MemFunPtr pFunction; Parm1 aParm1; Parm2 aParm2; }; template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1 > inline Functor * DeferCall ( const CalleePtr & pCallee, Ret(Callee::*pFunction)(Type1), const Parm1 & rParm1 ) { return new MemberFunctor1< CalleePtr, Ret(Callee::*)(Type1), Parm1 > (pCallee, pFunction, rParm1); } template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1, class Parm2> Functor * DeferCall2 ( const CalleePtr & pCallee, Ret(Callee::*pFunction)(Type1), const Parm1 & rParm1, const Parm2 & rParm2 ) { return new MemberFunctor2< CalleePtr, Ret(Callee::*)(Type1), Parm1, Parm2 > (pCallee, pFunction, rParm1, rParm2); } class TestObj { public: void test1 (int a) { printf("test 1 a=%d\r\n", a); } void test2 (int a, int b) { printf("test 2 a=%d, b=%d\r\n", a, b); } void test (void) { Functor * pCall = DeferCall(this, &TestObj::test1, 3); Functor * pCall1 = new MemberFunctor1 < TestObj *, void (TestObj::*)(int), int >(this, &TestObj::test1, 6); Functor * pCall2 = new MemberFunctor2 < TestObj *, void (TestObj::*)(int, int), int, int >(this, &TestObj::test2, 4, 5); Functor * pCall3 = DeferCall2(this, &TestObj::test2, 3, 3); // Compile time error. if (pCall) (*pCall)(); if (pCall1) (*pCall1)(); if (pCall2) (*pCall2)(); if (pCall3) (*pCall3)(); delete pCall; delete pCall1; delete pCall2; delete pCall3; } }; int _tmain(int argc, _TCHAR* argv[]) { TestObj *obj = new TestObj(); obj->test(); delete obj; return 0; }
原文:https://stackoverflow.com/questions/35798097
最满意答案
您确实应该使用
$1
,但是当您尝试运行脚本时,只需使用./your_script.sh your_argument
不要在开头添加%
。You should indeed use
$1
, but when you try to run your script just use./your_script.sh your_argument
don't add the%
in the beginning.
相关问答
更多-
如何通过命令行在shell脚本中传递参数(How to pass argument in expect through command line in shell script)[2022-08-30]
如果你想从参数中读取,你可以简单地通过 set username [lindex $argv 0]; set password [lindex $argv 1]; 并打印出来 send_user "$username $password" 该脚本将打印 $ ./test.exp user1 pass1 user1 pass1 您可以使用调试模式 $ ./test.exp -d user1 pass1 If you want to read from arguments, you can achiev ... -
简短的回答: spark-shell -i <(echo val theDate = $INPUT_DATE ; cat
) 长答案: 此解决方案会在传递给spark-submit之前在文件的开头添加以下行: val theDate = ... , 从而定义一个新变量。 这样做的方式( <( ... )语法)称为进程替换。 它在Bash中可用。 有关这方面的更多信息,请参阅此问题 ,以及非Bash环境的替代方案(例如mkFifo )。 使这更加系统化: 将下面的代码放在一个脚本中(例如 ... -
尝试: xargs --show-limits Your environment variables take up 2446 bytes POSIX upper limit on argument length (this system): 2092658 POSIX smallest allowable upper limit on argument length (all systems): 4096 Maximum length of command we could actually use: 2 ...
-
如何使shell脚本获取在命令行中传递的参数?(How to make a shell script take an argument passed in the command line?)[2022-07-01]
您确实应该使用$1 ,但是当您尝试运行脚本时,只需使用./your_script.sh your_argument不要在开头添加% 。 You should indeed use $1, but when you try to run your script just use ./your_script.sh your_argument don't add the % in the beginning. -
我的建议是将所有客户记录保存在一个csv文件中,您可以将值提取为: export a=`cat $BATCH_PROCESSES/input/$v_DateTime/$filename|head -$i|tail -1| awk -F ',' '{print $1}'` export b=`cat $BATCH_PROCESSES/input/$v_DateTime/$filename|head -$i|tail -1| awk -F ',' '{print $2}'` export c=`cat $BAT ...
-
这是一种更简单的方法来重现您的问题: $ echo "-e" "foo" foo # What happened to "-e"? echo解析您的预期输出作为选项是POSIX在可移植脚本中警告此命令的原因之一。 如果您尝试转储参数以进行日志记录和调试,则可以使用bash的printf %q : #!/bin/bash # (does not work with sh) printf '%q ' "$0" "$@" printf '\n' 这将以这样的方式转义输出参 ...
-
在shell脚本中将MySQL名称作为参数传递给MySQL命令行(Pass as argument a table name in a MySQL command line, in a shell script)[2021-08-04]
-e的参数必须是单个字符串。 您将表名作为desc的单独参数,因此它被视为数据库名称参数。 将其更改为: -e "desc $1" 这是我的整个脚本: #!/bin/bash if [ "$1" != "" ]; then mysql \ -A -D bbodb_test \ -e "desc $1" \ -B -N \ | awk 'BEGIN {ORS="\t"};{print $1}' | awk '{print "#"$0} ... -
使用printf %q生成参数列表的eval -safe字符串版本: # generate a string which evals to list of command-line parameters printf -v cmd_str '%q ' "$@" # pass that list of parameters on the remote shell's command line ssh "$host" "bash -s $cmd_str" <<'EOF' echo "This is run ...
-
尝试这个: eval $1 try this: eval $1
-
你可以这样做 ssh -oConnectTimeout=5 -oBatchMode=yes -l group servername bash -s -- -7 < /some/path/top_command.sh --是一个常见的选项 - 参数分隔符,在将以-开头的参数传递给命令时很有用。 像mv和rm这样的命令也可以识别它。 随后的所有内容--不再作为选项进行测试,并且已经被认为是正常的参数。 对于rm和mv ,如果文件以-开头,它会很有用。 You can do it like ssh -oConn ...