C++ 11引入方便的功能stoi,stol,stoll,stoul,stoull,stof,stod,和stold,其中一个字符串转换为整数,很久长,无符号长,无符号长长整型,浮点,双,或双长分别.
为什么不爱短暂和无条件的短片?
除了遗漏让我原谅我的事实外,我发现自己不得不在这样的情况下笨拙地工作:
#include <string>
struct S
{
S(short);
};
int main()
{
S s{std::stoi("4")};
}
Run Code Online (Sandbox Code Playgroud)
错误:
test.cpp: In function 'int main()':
test.cpp:10:23: error: narrowing conversion from 'int' to 'short int' inside { } [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我想反而写S s{std::stos("4")};,如果只有stos...
相反,我必须写S s{static_cast<short>(std::stoi("4"))};...哦等等,也不会这样做,它会默默地截断整数而不是短路,而不是一个假设的stos函数,如果整数不适合短路则抛出异常.所以基本上我回到我的预C++ 11层的替代品stringstreams,boost::lexical_cast等等.
编辑:既然人们似乎也很难定位我的实际问题,这是为什么没有stos和stous功能,以及其他的人去?
请考虑以下代码:
#include <vector>
class A
{
public:
A(A&&); // somewhat expensive
static std::vector<A> make_As()
{
std::vector<A> result;
result.push_back(A(3));
result.push_back(A(4));
return result;
}
private:
A(int); // private constructor
};
Run Code Online (Sandbox Code Playgroud)
因为A移动构造函数有些昂贵(无论出于何种原因),我想避免调用它并使用emplace_back():
#include <vector>
class A
{
public:
A(A&&); // somewhat expensive
static std::vector<A> make_As()
{
std::vector<A> result;
result.emplace_back(3);
result.emplace_back(4);
return result;
}
private:
A(int); // private constructor
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,emplace_back()实际的构造函数调用是由标准库中的某些东西完成的,它没有足够的特权来调用A私有构造函数.
我意识到可能没有什么可以做的,但是我觉得既然调用emplace_back()发生在一个成员中A,他们应该能够调用私有构造函数.
这有什么变通方法吗?
我唯一能想到的是添加一个friend-declaration A,但是需要成为A朋友的精确类(即实际尝试调用构造函数的类)是特定于实现的(例如,for海湾合作委员会(GCC __gnu_cxx::new_allocator<A>)). …
我的理解是程序可以动态链接到LGPL库并包含其标题,可能需要修改,而不必在LGPL下发布程序,但是必须释放对构建动态库的源代码的任何修改根据LGPL.
实际上,这允许人们无限制地使用库,但他们必须回馈他们对其所做的任何更改.
我想发布一个我作为FOSS编写的C++库,我想以同样的精神授权它:允许人们使用它而不必释放使用它的代码,但必须释放他们所做的任何更改它.但是,LGPL本身并不适合我,因为我的库完全是标题库(它是模板库).
什么许可证可以达到这个目的?
例如,我有一些功能模板
template <typename T>
void foo(T);
template <typename T>
void bar(T);
// others
Run Code Online (Sandbox Code Playgroud)
我需要将每一个传递给一个算法,该算法将用各种类型调用它,例如
template <typename F>
void some_algorithm(F f)
{
// call f with argument of type int
// call f with argument of type SomeClass
// etc.
}
Run Code Online (Sandbox Code Playgroud)
我无法传递我的函数模板未实例化,但我无法使用任何特定类型实例化它,因为some_algorithm需要使用几种不同类型的参数调用它.
我可以将我的函数模板调整为多态函数对象,例如
struct foo_polymorphic
{
template <typename T>
void operator()(T t)
{
foo(t);
}
};
Run Code Online (Sandbox Code Playgroud)
然后传递给它some_algorithm(foo_polymorphic()).但这需要为我的每个功能模板编写一个单独的适配器.
是否有一个通用的适应函数模板是一个多态函数对象,即一些机制,我可以重新使用的每一个我需要适应的功能模板,而不必为每一个单独声明什么方式?
在C++ 03中,模板参数推导在某些上下文中不会发生.例如:
template <typename T> struct B {};
template <typename T>
struct A
{
typedef B<T> type;
};
template <typename T>
void f(typename A<T>::type);
int main()
{
B<int> b;
f(b); // ERROR: no match
}
Run Code Online (Sandbox Code Playgroud)
这里int不推断T,因为嵌套类型A<T>::type是非推断的上下文.
我是否写过这样的函数:
template <typename T> struct B {};
template <typename T>
void f(B<T>);
int main()
{
B<int> b;
f(b);
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,因为B<T> 是推断的背景.
但是,在C++ 11中,模板别名可用于以类似于第二个示例的语法伪装嵌套类型.例如:
template <typename T> struct B {};
template <typename T>
struct A
{
typedef …Run Code Online (Sandbox Code Playgroud) 在重载解析期间,函数对象是否与常规函数区别对待?如果是这样,怎么样?
我遇到了以下情况:用等效可调用函数对象替换函数改变了代码的含义:
#include <iostream>
namespace N
{
enum E { A, B };
void bar(E mode) { std::cout << "N::bar\n"; }
}
template <typename... Args>
void bar(Args&&... args) { std::cout << "global bar\n"; }
int main()
{
bar(N::A);
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是"N :: bar".到目前为止,非常好:ADL找到N :: bar,N :: bar和全局条都是精确匹配,N :: bar是首选,因为它不是模板.
但是如果我将全局条改变为函数对象,就像这样:
#include <iostream>
namespace N
{
enum E { A, B };
void bar(E mode) { std::cout << "N::bar\n"; }
}
struct S
{
template <typename... Args>
void operator()(Args&&... args) { std::cout …Run Code Online (Sandbox Code Playgroud) C++标准库的max_element算法需要将迭代器作为模型的输入传递ForwardIterator.
我的理解是通过指定您可以使用a 多次迭代相同的范围来ForwardIterator提炼.因此,多遍算法需要s.InputIteratorForwardIteratorForwardIterator
但是,max_element不是多遍算法 - 只需迭代一次范围就可以确定其最大元素.那么为什么max_element需要额外的功能ForwardIterator呢?
C++ 11有一些线程概念.例如,它定义了一个新的存储说明符thread_local,并为具有此存储说明符的变量指定"每个线程有一个不同的对象或引用"[basic.stc.thread].
什么被认为是为此目的的"线程"?是否只使用标准线程库创建的线程(即由std::thread对象表示的线程)?通过其他方式创建的线程怎么样(例如,直接在Linux上使用pthreads)?如果我使用提供用户空间线程的库,那么每个人都会获得自己的thread_local对象副本(我真的不知道如何实现)?
如果答案是"它的实现定义了什么被认为是用于诸如此类的目的的线程thread_local",那么有人可以举例说明一个众所周知的实现如何定义这个吗?
c++ multithreading language-lawyer thread-local-storage c++11
我正在尝试编写一个名为signature_of的元函数,给定函数(指针),仿函数或lambda的类型,返回其签名.
这是我到目前为止所拥有的:
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/function_types/is_member_function_pointer.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <type_traits>
template <typename F>
struct signature_of_member
{
typedef typename boost::function_types::result_type<F>::type result_type;
typedef typename boost::function_types::parameter_types<F>::type parameter_types;
typedef typename boost::mpl::pop_front<parameter_types>::type base;
typedef typename boost::mpl::push_front<base, result_type>::type L;
typedef typename boost::function_types::function_type<L>::type type;
};
template <typename F, bool is_class>
struct signature_of_impl
{
typedef typename boost::function_types::function_type<F>::type type;
};
template <typename F>
struct signature_of_impl<F, true>
{
typedef typename signature_of_member<decltype(&F::operator())>::type type;
};
template <typename F>
struct signature_of
{
typedef typename signature_of_impl<F, std::is_class<F>::value>::type type; …Run Code Online (Sandbox Code Playgroud) 假设我有一个管理内存的类,因此需要用户定义的特殊成员函数(想象vector或类似).
考虑move-assignment运算符的以下实现:
Class& operator=(Class&& rhs)
{
this->~Class(); // call destructor
new (this) Class(std::move(rhs)); // call move constructor in-place
}
Run Code Online (Sandbox Code Playgroud)
以这种方式实现移动赋值运算符是否有效?也就是说,以这种方式调用析构函数和构造函数是否与语言中的任何对象生存规则相冲突?
以这种方式实现移动赋值运算符是一个好主意吗?如果没有,为什么不,并且有更好的规范方式?