为什么在模板元编程中使用枚举而不是静态const bool被认为是更好的做法?
我在亚历山德列斯库的书中读到过这个但是找不到它,真的很想知道它.
是否有人知道根据下面的规则代码不编译?
template <class T>
struct B
{
typedef T type;
};
template<class T>
struct X
{
};
template<class T>
struct X<B<T>::type*>//HERE I'M PARTIALLY SPECIALIZING (WELL, TRYING TO...)
{
};
Run Code Online (Sandbox Code Playgroud)
请参阅代码中的注释.
为什么这个(char在我的实现上签名):
cout << std::is_same< char,signed char>::value;
输出错误?
怎么可能"分裂"即长长型,所以它的第一部分是长型的变种而第二部分也是长型的变种.
long long long_type = 0xaaaabbbbccccdddd;
Run Code Online (Sandbox Code Playgroud)
并且在第一个int中我想要long_type var的前半部分(从哪一侧无关紧要)和第二个var int的后半部分.
此代码在VS2015更新1中给出了错误:
错误C2893:无法专门化函数模板'unknown-type std :: invoke(_Callable &&,_ Types && ...)'
#include <iostream>
#include <functional>
using std::cout;
class A
{
public:
virtual void init()
{
cout << "A";
};
};
class B
{
public:
virtual void init()
{
cout << "B";
};
};
class C : private A, private B
{
std::function<void()> a_init = &A::init;
std::function<void()> b_init = &B::init;
public:
void call()
{
a_init();
b_init();
}
};
int main()
{
C c;
c.call();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何想法,如果VS编译器是错误或我的代码?
编辑
#include "stdafx.h"
#include <functional> …Run Code Online (Sandbox Code Playgroud) 我试图赶上零尝试:
int _tmain(int argc, _TCHAR* argv[])
{
int a = 5;
try
{
int b = a / 0;
}
catch(const exception& e)
{
cerr << e.what();
}
catch(...)
{
cerr << "Unknown error.";
}
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上它不起作用.有什么建议吗?谢谢.PS将来代码可以放在[code] [/ code]标签而不是四个空格之间的任何机会?
什么是更好的编码实践:如果我必须有一个try/catch块,我应该将所有内容(每次初始化等)放在这个块中,还是只放置那些可能抛出的变量?这两种结构有什么区别吗?
例如:
拥有:
struct A {
A();
int a;
int* b;
};
Run Code Online (Sandbox Code Playgroud)
然后在.cpp中:
A::A() {
a = 5;
try {
b = new int;
}
catch(...){
}
}
Run Code Online (Sandbox Code Playgroud)
要么
A:A() {
try {
a = 5; //this time in try block
b = new int;
}
catch(...) {
}
}
Run Code Online (Sandbox Code Playgroud)
这两个结构之间是否有任何区别,或者如果我必须有一个try/catch块,我可能会把所有内容放入其中吗?谢谢.
PS为了上帝的缘故,这里的格式化让我真的很疯狂!而且我知道我多次提到这一点,我不会生气.