我在看这个源代码
template<char... digits>
struct conv2bin;
template<char high, char... digits>
struct conv2bin<high, digits...> {
static_assert(high == '0' || high == '1', "no bin num!");
static int const value = (high - '0') * (1 << sizeof...(digits)) +
conv2bin<digits...>::value;
};
template<char high>
struct conv2bin<high> {
static_assert(high == '0' || high == '1', "no bin num!");
static int const value = (high - '0');
};
template<char... digits>
constexpr int operator "" _b() {
return conv2bin<digits...>::value;
}
int array[1010_b];
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是有效的C++.
template<char high, char... digits> …Run Code Online (Sandbox Code Playgroud) c++ static-assert user-defined-literals variadic-templates c++11
将可变参数模板的参数包含在初始化程序列表中应该确保它们按顺序进行评估,但不会在此处进行:
#include <iostream>
using namespace std;
template<class T> void some_function(T var)
{
cout << var << endl;
}
struct expand_aux {
template<typename... Args> expand_aux(Args&&...) { }
};
template<typename... Args> inline void expand(Args&&... args)
{
bool b[] = {(some_function(std::forward<Args>(args)),true)...}; // This output is 42, "true", false and is correct
cout << "other output" << endl;
expand_aux temp3 { (some_function(std::forward<Args>(args)),true)... }; // This output isn't correct, it is false, "true", 42
}
int main()
{
expand(42, "true", false);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
怎么会?
这很奇怪..下面的代码(我设法编译,感谢Cassio Neri)正在编译而没有任何错误..顺便说一下hashing_func和key_equal_func都被调用(cout没有显示在控制台窗口中)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <functional>
using namespace std;
unsigned long hashing_func(string key)
{
cout << "Hashing called";
unsigned long hash = 0;
for(int i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
template<class T> bool key_equal_fn(T t1, T t2)
{
return t1 == t2;
}
template <> bool key_equal_fn<string>(string t1, string t2)
{
cout << "Equal called";
return !(t1.compare(t2));
}
int main ()
{
unordered_map<string, …Run Code Online (Sandbox Code Playgroud) 我知道我可以获取指向类或结构的数据成员的指针,但是下面代码的最后一行无法编译:
struct abc
{
int a;
int b;
char c;
};
int main()
{
typedef struct abc abc;
char abc::*ptt1 = &abc::c;
void *another_ptr = (void*)ptt1;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能将ptt1转换为another_ptr?我们正在谈论指针,所以一个指针应该具有与另一个指针相似的尺寸(虽然在概念上不同)
我以为我可以指向一个完全专用的模板函数,但下面的代码没有编译(MSVC2012)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
unsigned long hashing_func(string key)
{
unsigned long hash = 0;
for(int i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
bool key_equal_fn2(string t1, string t2)
{
return t1 == t2;
}
template<class T> bool key_equal_fn(T t1, T t2)
{
return t1 == t2;
}
template <> bool key_equal_fn<string>(string t1, string t2)
{
return !(t1.compare(t2));
}
int main ()
{
unordered_map<string, string>::size_type n …Run Code Online (Sandbox Code Playgroud) variadic模板printf函数有几种实现方式.一个是这样的:
void printf(const char* s) {
while (*s) {
if (*s == '%' && *++s != '%')
throw std::runtime_error("invalid format string: missing arguments");
std::cout << *s++;
}
}
template<typename T, typename... Args>
void printf(const char* s, const T& value, const Args&... args) {
while (*s) {
if (*s == '%' && *++s != '%') {
std::cout << value;
return printf(++s, args...);
}
std::cout << *s++;
}
throw std::runtime_error("extra arguments provided to printf");
}
Run Code Online (Sandbox Code Playgroud)
并且到处都说这个实现是类型安全的,而普通的C(具有可变参数va_arg)则不是.
这是为什么?类型安全是什么意思,这个实现相对于C printf va_arg有什么优势?
我想知道当我有这样的函数时会发生什么:
typedef std::unordered_map<std::string,std::string> stringmap;
stringmap merge (stringmap a,stringmap b)
{
stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}
Run Code Online (Sandbox Code Playgroud)
函数返回时会发生什么?
'temp'是否在被销毁之前复制到临时r值(超出范围)并且C++ 11可能是NRVO-ptimized(因此'temp'的副本直接写入返回目标槽)?
在可变参数模板中,...运算符将参数包扩展为一系列以逗号分隔的参数(最简单的形式).我的问题是:为什么调用some_function()为多个参数逗号分隔的作品,并用...运算符调用它不?
我在谈论这段代码:
template<typename... Args> inline void expand(Args&&... args)
{
some_function(22),some_function(32); // Works
some_function(args)...; // Doesn't work - ERROR
}
Run Code Online (Sandbox Code Playgroud)
这两条线不应该产生类似的输出吗?
我有一个简单的结构,我想要一个指向成员的指针c.我正在使用MSVC2012,如果我没有将struct abc声明为类型定义(typedef),我就不能使用它..怎么来的?
struct abc
{
int a;
int b;
char c;
};
char (struct abc)::*ptt1 = &(struct abc)::c; // Error: error C2144: syntax error : 'abc' should be preceded by ')'
typedef struct abc;
char abc::*ptt1 = &abc::c; // Compiles just fine
Run Code Online (Sandbox Code Playgroud) 为什么这段代码失败了
错误C2893:无法专门化函数模板''unknown-type'makeAndProcessObject(const Builder&)'
我正在使用MSVC2012
class BBuilder
{
public:
int makeObject()
{
return 22;
}
};
template <typename Builder>
auto
makeAndProcessObject (const Builder& builder) -> decltype( builder.makeObject() )
{
auto val = builder.makeObject();
// do stuff with val
return val;
}
int main()
{
BBuilder myobj;
auto retval = makeAndProcessObject(myobj);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(实例)