作为仿函数模板的模板参数(template argument as functor template)
我试图创建一个线程类,从适配器执行一个仿函数。 该代码显示了我的尝试。
#include <iostream> struct null_t { }; typedef void (*thread_func_t)(); typedef void (*thread_func_2_t)(int); template <typename F, typename P = null_t> class adapter { public: adapter(F f, P p = null_t()) : _f(f), _p(p) {} void operator ()() { _f(_p); } private: F _f; P _p; }; template <typename T> class thread { public: explicit thread(T f) : _f(f) { } void run() { _f(); } private: T _f; }; void show_hello() { std::cout << "hello" << std::endl; } void show_num(int x) { std::cout << "show_num: " << x << std::endl; } int main() { thread<adapter<thread_func_t> > t_a(adapter<thread_func_t>(&show_hello)); t_a.run(); int i = 666; thread<adapter<thread_func_2_t, int> > t_b(adapter<thread_func_2_t, int>(&show_num, i)); t_b.run(); }
编译器错误:
$ /usr/bin/g++-4.4 func.cpp -o func func.cpp: In function ‘int main()’: func.cpp:51: error: request for member ‘run’ in ‘t_a’, which is of non-class type ‘thread<adapter<void (*)(), null_t> >(adapter<void (*)(), null_t>&)’
1)
adapter
不准备调用没有参数的函数。 (我不知道该怎么做)。2)我已经尝试过
thread
接收模板模板参数但没有成功。我试图做几乎相同的样本波纹管(此适配器不适用于没有参数的函数):
typedef void (*WorkerFunPtr)(const std::string&); template<typename FunT, typename ParamT> struct Adapter { Adapter(FunT f, ParamT& p) : f_(f), p_(&p) {} void operator( )( ) { f_(*p_); } private: FunT f_; ParamT* p_; }; void worker(const std::string& s) { std::cout << s << '\n'; } int main( ) { std::string s1 = "This is the first thread!"; boost::thread thr1(Adapter<WorkerFunPtr, std::string>(worker, s1)); thr2.join( ); }
I'm trying to create a thread class which executes a functor from an adapter. The code shows my attempt.
#include <iostream> struct null_t { }; typedef void (*thread_func_t)(); typedef void (*thread_func_2_t)(int); template <typename F, typename P = null_t> class adapter { public: adapter(F f, P p = null_t()) : _f(f), _p(p) {} void operator ()() { _f(_p); } private: F _f; P _p; }; template <typename T> class thread { public: explicit thread(T f) : _f(f) { } void run() { _f(); } private: T _f; }; void show_hello() { std::cout << "hello" << std::endl; } void show_num(int x) { std::cout << "show_num: " << x << std::endl; } int main() { thread<adapter<thread_func_t> > t_a(adapter<thread_func_t>(&show_hello)); t_a.run(); int i = 666; thread<adapter<thread_func_2_t, int> > t_b(adapter<thread_func_2_t, int>(&show_num, i)); t_b.run(); }
The compiler error:
$ /usr/bin/g++-4.4 func.cpp -o func func.cpp: In function ‘int main()’: func.cpp:51: error: request for member ‘run’ in ‘t_a’, which is of non-class type ‘thread<adapter<void (*)(), null_t> >(adapter<void (*)(), null_t>&)’
1) The
adapter
is not prepared to call a function without parameter. (I dont know how to do that).2) I've tried
thread
receiving a template template parameter without success.I'm trying to do almost the same as the sample bellow (this Adapter does not work with function that does not have parameter):
typedef void (*WorkerFunPtr)(const std::string&); template<typename FunT, typename ParamT> struct Adapter { Adapter(FunT f, ParamT& p) : f_(f), p_(&p) {} void operator( )( ) { f_(*p_); } private: FunT f_; ParamT* p_; }; void worker(const std::string& s) { std::cout << s << '\n'; } int main( ) { std::string s1 = "This is the first thread!"; boost::thread thr1(Adapter<WorkerFunPtr, std::string>(worker, s1)); thr2.join( ); }
原文:https://stackoverflow.com/questions/8009879
最满意答案
将NLS_NUMERIC_CHARACTERS设置为“,”。 固定发布的手册说明:
如果已使用初始化参数NLS_NUMERIC_CHARACTERS建立了句点(。)以外的十进制字符,则必须使用“text”表示法指定数字文字。 在这些情况下,Oracle会自动将文本文字转换为数字值。
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm
Oracle 10.2g确认了此行为。
Setting NLS_NUMERIC_CHARACTERS to ",." fixed the issued as the manual states:
If you have established a decimal character other than a period (.) with the initialization parameter NLS_NUMERIC_CHARACTERS, then you must specify numeric literals with 'text' notation. In these cases, Oracle automatically converts the text literal to a numeric value.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm
This behaviour is confirmed with Oracle 10.2g.
相关问答
更多-
从来没有听说过这个问题。 您是否尝试从查询的Integers / Floats中删除任何引号: $sql = "SELECT city, state,((ACOS(SIN(38.2965709 * PI() / 180) * SIN(lat * PI() / 180) + COS(38.2965709 * PI() / 180) * COS(lat * PI() / 180) * COS((-77.6340366-lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 ...
-
Zend 2.0 Select查询不起作用,但原始SQL中的相同查询有效(Zend 2.0 Select query not working but the same query in raw SQL works)[2023-07-25]
变量未在闭包上下文中设置 The variables are not set in the closure context -
我想你不需要提到用户名和密码 resources.db.adapter = "sqlsrv" resources.db.host = "localhost\SQLEXPRESS" resources.db.dbname = "DatabaseName" resources.db.isDefaultTableAdapter = true resources.db.driver_options.ReturnDatesAsStrings = true I ended up writing my own Ada ...
-
我认为您需要更改您的select语句,以便将MATCH AGAINST部分从order子句移动到字段列表中。 例如 代替 SELECT * FROM mytable WHERE match(text,phone,phone2) AGAINST ('something') ORDER BY match(text,phone,phone2) AGAINST ('something'); 你将会拥有 SELECT mytable.*, match(text,phone,phone2) AGAINST ('som ...
-
出于一些奇怪的原因,当我将第一个例子中的$ url从“https:// .....”更改为“http:// .....”时,它起作用了。 这很奇怪,因为https应该可以工作,但我把它留在这里可能有同样问题的任何人。 For some weird reason, when i changed the $url in the first example from "https://....." to "http://....." it worked. This is weird as https is sup ...
-
$select = $this->_db->select() ->from('OSLANG') ->join('OS',"OSLANG.OSLANG_ID = OS.OSLANG_ID") ->where("OSSUBVERSION_ID = ?", $subVersionId); $results = $select->query()->fetchAll(); $select = $this-> ...
-
不是Paginator工厂的重点,你也可以将它传递给行集,它也会分页吗? 我刚试了这个,它似乎适合我(即使我通常使用Zend_Db_Select) $db = Zend_Db_Table::getDefaultAdapter(); $rowset = $db->query('SELECT * FROM user')->fetchAll(); $paginator = Zend_Paginator::factory($rowset); Isn't the point of the Paginator fac ...
-
Zend更新...返回...进入(Zend update … returning … into)[2023-05-03]
我认为你使用Zend_Db_Adapter_Oracle适配器? 由于Zend_Db_Statement_Oracle内部使用oci_bind_by_name() ,因此可以处理IN和OUT参数。 对于OUT参数,应始终设置类型。 基本上,你可以简单地用你的陈述来做这件事 $stmt->bindParam('bind_var', $counter, SQLT_INT); I take it you're using the Zend_Db_Adapter_Oracle adapter? As Zend_D ... -
当值1未加引号时,使用带有Zend的select不返回结果(Using select with Zend not returning results when value 1 is unquoted)[2023-05-26]
将NLS_NUMERIC_CHARACTERS设置为“,”。 固定发布的手册说明: 如果已使用初始化参数NLS_NUMERIC_CHARACTERS建立了句点(。)以外的十进制字符,则必须使用“text”表示法指定数字文字。 在这些情况下,Oracle会自动将文本文字转换为数字值。 https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm Oracle 10.2g确认了此行为。 Setting NLS_NUMERIC ... -
正如扎克所写,你错过了 $paginator->setItemCountPerPage(10); $paginator->setCurrentPageNumber(1); 您可以创建一个视图脚本来呈现分页器控件和分页器中的项目。 比你必须打电话 $paginator->getPages()->pageCount 如果您没有使用Zend_View并且它是paginatorControl视图助手。 As Zak wrote, you are missing $paginator->setItemCountP ...