我正在阅读C++模板完整指南,并遇到了这个非类型函数模板参数代码(我添加了main()和除函数定义和调用之外的其他部分):
#include <vector>
#include <algorithm>
#include <iostream>
template <typename T, int value>
T add (T const & element){
return element + value;
}
int main() {
int a[] = {1,2,3,4};
int length = sizeof (a) / sizeof (*a);
int b[length];
std::transform (a, a + length, b, (int(*)(int const &))add <int, 5>); //why?
std::for_each (b, b + length, [](int const & value){ std::cout << value << '\n'; });
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从书中读到为什么我们需要对函数调用进行类型转换后我才明白?
编辑:书中的解释:
add是一个函数模板,函数模板被认为是一组重载函数(即使集合只有一个成员).但是,根据当前标准,多组重载函数不能用于模板参数推导.因此,您必须转换为函数模板参数的确切类型:...
编译器:Ubuntu 10.10上的g ++ 4.5.1
我有一个关于WndProc在我的表单应用程序中使用的基本问题.我想知道WndProc当我有表单事件可用时该方法的用途.在哪些情况下我需要创建自定义消息?MSDN告诉它它仅用于处理Windows消息.
我试图将unsigned int与这样的signed char进行比较:
int main(){
unsigned int x = 9;
signed char y = -1;
x < y ? printf("s") : printf("g");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待o/p为"g".相反,它的"s".这里进行了什么样的转换?
我正在cin使用lambda表达式尝试在循环索引中使用循环索引的值:
#include<iostream>
using namespace std;
int main(){
for(int a, ([](int & b){cin>>b;})(a); a < 2; ++a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这些是我在ubuntu上使用g ++ 4.5编译时的错误:
forLoopAndCinTest.c++: In function ‘int main()’:
forLoopAndCinTest.c++:5:14: error: expected unqualified-id before ‘[’ token
forLoopAndCinTest.c++:5:14: error: expected ‘)’ before ‘[’ token
forLoopAndCinTest.c++:5:34: error: expected primary-expression before ‘)’ token
forLoopAndCinTest.c++:5:34: error: expected ‘;’ before ‘)’ token
forLoopAndCinTest.c++:5:40: error: name lookup of ‘a’ changed for ISO ‘for’ scoping
forLoopAndCinTest.c++:5:40: note: (if you use ‘-fpermissive’ G++ will accept your code)
forLoopAndCinTest.c++:5:50: …Run Code Online (Sandbox Code Playgroud) 我正在阅读http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/并遇到此代码以检查类型是否为指针:
template<class T> struct
isPtr {
static const bool value = false;
};
template<class U> struct
isPtr<U*> {
static const bool value = true;
};
template<class U> struct
isPtr<U * const> {
static const bool value = true;
};
Run Code Online (Sandbox Code Playgroud)
我如何专门化通用模板来处理const指针到const类型的情况?如果我这样做:
std::cout << isPtr <int const * const>::value << '\n';
Run Code Online (Sandbox Code Playgroud)
当我期待虚假时,我得到一个真实的.谁能解释一下?
编辑:使用VC++ 2010编译器(快递:-)
我有以下函数,就像一个索引操作符:
let {
index :: [a]->Int->Maybe a
index [] i = error "Empty list"
index l i = if i <= ((length l) - 1) && i >= 0 then
Just(l !! i)
else
error "Index out of bounds"
}
Run Code Online (Sandbox Code Playgroud)
现在,我最初在没有使用的情况下编写了这个Just(我仍然不明白google搜索后它是什么):
let {
index :: [a]->Int->Maybe a
index [] i = error "Empty list"
index l i = if i <= ((length l) - 1) && i >= 0 then
(l !! i)
else
error "Index out of …Run Code Online (Sandbox Code Playgroud) 我正在尝试刷新Excel工作表中的数据透视表并获得以下异常:
Item method in the PivotTables class failed
Run Code Online (Sandbox Code Playgroud)
下面是代码:
pivotSheet.Activate();
Microsoft.Office.Interop.Excel.PivotTables pivotTables =
(Microsoft.Office.Interop.Excel.PivotTables)pivotSheet.PivotTables(missing);
int pivotTablesCount = pivotTables.Count;
if (pivotTablesCount > 0)
{
for (int i = 0; i <= pivotTablesCount; i++)
{
pivotTables.Item(i).RefreshTable(); //The Item method throws an exception
}
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我提出了以下尾递归斐波纳契生成器:
let {
fibo :: Integral x => [x]->x->x->x->[x]
fibo l x y 0 = l
fibo l x y n = fibo (l ++ [y+x] ++ [y+x+y]) (x+y) (y+x+y) (n-1)
}
Run Code Online (Sandbox Code Playgroud)
请原谅我把整个实现放在一行,因为我使用GHCi并且还没有完全学会如何把它放在一个文件中并运行(我还没到达那里).我想知道的是这个电话:
fibo [0, 1] 0 1 5
Run Code Online (Sandbox Code Playgroud)
可以改进.我不想用0和1传递初始列表,然后再次使用限制传递0和1.我相信可以改变实施.可以做些什么改变?
我遇到了这段代码:
if (1, true) {/*...*/}
Run Code Online (Sandbox Code Playgroud)
它究竟意味着什么?虽然评估结果为true:
void foo(){}
...
if(1, foo()) {/*...*/}
Run Code Online (Sandbox Code Playgroud)
这不编译:
void foo(){}
...
if (1 == foo()) {/*...*/}
Run Code Online (Sandbox Code Playgroud)
显然因为编译器期望foo()返回一些整数值.我以为逗号会转换为某个运算符.if子句中的逗号是否转换为内部内容?
如何使用VBA脚本中的Selection对象从word文档中获取一行?像这样的东西:
Selection.MoveDown Unit:=wdLine, Count:=15
'print the 15th line here
Run Code Online (Sandbox Code Playgroud)
编辑:当我这样做:
Selection.MoveDown Unit:=wdLine, Count:=15
MsgBox (Selection.Text)
Run Code Online (Sandbox Code Playgroud)
它只打印该行的第一个字符.