我正在使用这个方法http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl.html将C++模板函数的定义与其声明分开,以避免用代码混乱我的头文件.
该链接使用一个没有参数或返回的函数作为示例,但假设我有一个带参数的函数.该链接将建议以下安排:
// File "f.h"
template <typename T> void f(T t);
Run Code Online (Sandbox Code Playgroud)
// File "f.cpp"
#include "f.h"
template <typename T> void f(T t) {
// do something
}
template void f<int>(int t);
// other specializations as needed
Run Code Online (Sandbox Code Playgroud)
但是,如果省略尖括号中的类型,似乎专门化也有效,因为我认为编译器从参数类型推导出它:
template void f(int t);
Run Code Online (Sandbox Code Playgroud)
但我想知道,这样做有效吗?
Visual C++ 12(2013)
使用Visual Studio Express 2013.我以为我理解静态与动态数组,但以下让我困惑:
int* a = new int[3]; \\ dynamic array
int** pa = &a;
int b[3]; \\ static array
int** pb = &b; \\ error: cannot convert from 'int (*)[3]' to 'int **'
Run Code Online (Sandbox Code Playgroud)
好吧,所以我尝试int (*)[3] pb = &b;
但它甚至在语法上都不正确.本地窗口说pb是一个int[3]
,但int[3]* pb = &b;
也是不正确的.
它似乎也奇怪,我b
有相同的值pa
.如果我用int b[3];
声明+初始化替换声明int b[3] = {};
那么奇怪就会消失,所以也许它只是一个VS错误:
但我仍然无法获得静态数组的地址.如果我输入&b
立即窗口然后我得到完全相同的输出,就像我刚输入一样b
,与'&a'和'a'相比,这看起来很奇怪,这些明显不同:
我正在尝试使用模板来创建type_info::name()
发出const
限定名称的函数的模拟.例如typeid(bool const).name()
,"bool"
但我想看"bool const"
.所以对于泛型类型我定义:
template<class T> struct type_name { static char const *const _; };
template<class T> char const *const type_name<T>::_ = "type unknown";
char const *const type_name<bool>::_ = "bool";
char const *const type_name<int>::_ = "int";
//etc.
Run Code Online (Sandbox Code Playgroud)
然后type_name<bool>::_
是"bool"
.对于非const类型,显然我可以为每种类型添加一个单独的定义,等等char const *const type_name<bool const>::_ = "bool const";
.但是我想我会尝试使用部分特化和连接宏来在一行中派生任何类型的const限定名称const
- 先前定义的资格名称.所以
#define CAT(A, B) A B
template<class T> char const *const type_name<T const>::_
= CAT(type_name<T>::_, " const"); // …
Run Code Online (Sandbox Code Playgroud) 这似乎不一致.我f
为签名类型重载了3个函数short
,int
并且long long
.如果你传递了一个,unsigned short
那么它会被提升为下一个最大的签名类型int
.但是,如果你通过unsigned int
它然后它没有被提升到签名long long
这是我所期望的,而是编译器抱怨对重载函数的模糊调用.
void f(short x) { std::printf("f(short)\n"); }
void f(int x) { std::printf("f(int)\n"); }
void f(long long x) { std::printf("f(long long)\n"); }
int main()
{
f((unsigned short)0); // Fine: calls f(int)
// f((unsigned int)0); // Ambiguous: could be f(short), f(int) or f(long long)
}
Run Code Online (Sandbox Code Playgroud) 当我用一个对象做很多不同的事情时,我经常使用 With object 语法。它是调用对象的属性/方法的有用简写,而不必因重用对象的名称而使代码变得混乱。然而有时我想调用一个将对象本身作为参数的函数。在这种情况下有什么方法可以引用该对象吗?
' Class Module
' Class1
' Code Module
Sub f(byref obj as Class1)
End Sub
Sub test()
Dim obj as Class1: set obj = new Class1
With obj
f Me ' Doesn't work - can you refer to obj in this context?
f obj ' Works but I don't like it when the object has a long name
End With
End Sub
Run Code Online (Sandbox Code Playgroud) #include <iostream>
class C {
public:
~C() { std::cout << this << " destructor\n"; }
C() { std::cout << this << " constructor\n"; }
C(C&& rhs) {
std::cout << &rhs << " rhs\n";
std::cout << this << " move constructor\n";
}
C& operator=(C&& rhs) {
std::cout << &rhs << " rhs\n";
std::cout << this << " move assignment\n";
return *this;
}
};
C make_one() {
C tmp;
return tmp;
}
int main() {
std::cout << "move constructor:\n";
C c1(make_one());
std::cout …
Run Code Online (Sandbox Code Playgroud) Lisp noob在这里.
CL-USER> (defun my-if (a b c)
(cond (a b)
(t c)))
CL-USER> (my-if t (print 1) (print 2))
1
2
1
Run Code Online (Sandbox Code Playgroud)
我没想到得到2,因为cond
如果第一个为真,则不应该评估第二个子句:
CL-USER> (cond (t (print 1))
(t (print 2)))
1
1
Run Code Online (Sandbox Code Playgroud)
这是为什么我们需要宏,还是我犯了其他错误?