在这个答案中,我根据类型的is_arithmetic
属性定义了一个模板:
template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){
return to_string(t);
}
template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){
return static_cast<ostringstream&>(ostringstream() << t).str();
}
Run Code Online (Sandbox Code Playgroud)
dyp建议,不是is_arithmetic
类型的属性,是否to_string
为类型定义是模板选择标准.这显然是可取的,但我不知道如何说:
如果
std::to_string
未定义,则使用ostringstream
重载.
声明to_string
标准很简单:
template<typename T> decltype(to_string(T{})) stringify(T t){
return to_string(t);
}
Run Code Online (Sandbox Code Playgroud)
这与我无法弄清楚如何构建的标准相反.这显然不起作用,但希望它传达了我正在尝试构建的内容:
template<typename T> enable_if_t<!decltype(to_string(T{})::value, string> (T t){
return static_cast<ostringstream&>(ostringstream() << t).str();
}
Run Code Online (Sandbox Code Playgroud) 假设我想引用initializer_list
我已定义的成员.我可以做吗?
这段代码在Visual Studio和gcc中编译并给出了预期的"13 55" ,我只想知道它是合法的:
const int foo[2] = {13, foo[0] + 42};
Run Code Online (Sandbox Code Playgroud) c++ arrays initializer-list language-lawyer aggregate-initialization
我发布了这个答案:https://stackoverflow.com/a/28459180/2642059其中包含以下代码:
void foo(string&& bar){
string* temp = &bar;
cout << *temp << " @:" << temp << endl;
}
Run Code Online (Sandbox Code Playgroud)
是bar
左值还是左值?
我问,因为我显然不能取rvalue的地址,但我可以在这里完成rvalue引用的地址.
如果你可以对左值参考执行任何操作,你可以在左值参考上区分用"&&"而不仅仅是"&"来区分两者?
可以在运行时创建C-Strings还是std::string
必须创建constexpr
它们?
使用gcc 4.9.2我可以这样做:
constexpr const char foo[] = "blee";
Run Code Online (Sandbox Code Playgroud)
(遗憾的是,2013年11月的客户技术预览版不允许Visual Studio支持此功能:https://stackoverflow.com/a/29255013/2642059)
但即使使用gcc 4.9.2我也不能这样做:
constexpr const std::string foo = "blee";
Run Code Online (Sandbox Code Playgroud)
我收到错误:
error: the type 'const string {aka const std::basic_string<char>}' of constexpr variable 'foo'
is not literal
constexpr const std::string foo = "blee";
^
note: 'std::basic_string<char>' is not literal because:
class basic_string
^
note: 'std::basic_string<char>' has a non-trivial destructor
Run Code Online (Sandbox Code Playgroud)
但我想更多地澄清为什么 a std::string
不是文字.也就是说:为什么必须在运行时构造字符串?
正如所指出的,这个问题可以部分回答:是否可以在constexpr中使用std :: string?但它没有涉及为什么std::string
不能成为问题核心的文字.
我得到了一段void()
用作参数的代码.代码不编译......很明显?
我们可以实例化任何类型的东西void
吗?我相信答案是否定的,除了a void*
.例如:
void askVoid(void param) {}
错误:参数可能没有
void
类型
void askNaught() {}
并使用askNaught(void())`错误调用它:错误C2660 ::
takeNaught
函数不带1个参数
template <typename T> void takeGeneric(T param) {}
并调用它有takeGeneric(void())
错误:错误C2893:无法专门化功能模板
void takeGeneric(T)
void voidType
错误:不允许使用不完整的类型
auto autoVoid = void()
错误:不能推断出
auto
类型
void* voidPtr
工作正常,但remove_pointer_t<decltype(voidPtr)> decltypeVoid
错误:错误C2182 ::
decltypeVoid
非法使用类型void
就是这样,对吧?void()
C++中没有地方可供选择吗?这只是我给出的错误代码,对吧?
C++ 11允许在以下标准中使用标准布局类型union
:Union的成员具有用户定义的构造函数
我的问题是:我是否保证在union
超出范围时会调用自定义析构函数?
我的理解是我们必须在切换时手动销毁和构建:http://en.cppreference.com/w/cpp/language/union#Explanation
但是像这样的例子怎么样:
{
union S { string str;
vector<int> vec;
~S() {} } s = { "Hello, world"s };
}
Run Code Online (Sandbox Code Playgroud)
当s
超出范围时,我是否将内存泄漏到堆上分配的字符串,因为我没有调用string
析构函数?
鉴于string foo
,我已经写了关于如何使用's 将字符转换为小写的答案cctype
tolower
transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))
Run Code Online (Sandbox Code Playgroud)
但我已经开始考虑 locale
的tolower
,这可以这样使用:
use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
Run Code Online (Sandbox Code Playgroud)
tolower
接受并返回一个int
我认为只是一些过时的C东西的事实?StackOverflow和其他地方有很多声明nth_element
是O(n),它通常用Introselect实现:http://en.cppreference.com/w/cpp/algorithm/nth_element
我想知道如何实现这一目标.我查看了维基百科对Introselect的解释,这让我更加困惑.算法如何在QSort和Median-of-Medians之间切换?
我在这里找到了Introsort论文:http://citeseerx.ist.psu.edu/viewdoc/download?doi = 10.1.1.14.5196 &rep = rep1&type = pdf 但是这说:
在本文中,我们将集中讨论排序问题,并在后面的章节中简要回到选择问题.
我试图通过STL本身来了解如何nth_element
实现,但这很快就会变得毛茸茸.
有人能告诉我如何实现Introselect的伪代码吗?或者甚至更好,当然除了STL之外的实际C++代码:)
在C++ 11 array
,string
和vector
所有得到了data
方法,该方法:
返回指向用作元素存储的基础数组的指针.指针是范围[
data()
;data() + size()
)即使容器为空,也始终是有效范围.[ 来源 ]
此方法const
以所有适用容器的可变和版本提供,例如:
T* vector<T>::data();
const T* vector<T>::data() const;
Run Code Online (Sandbox Code Playgroud)
所有适用的容器,也就是除了string
其只提供const
版本:
const char* string::data() const;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?为什么会被string
贬低,何时char* string::data()
会如此有用?
所以我很困惑这是如何工作的.鉴于:
template <typename T>
int foo(T t) { t.foo(); }
Run Code Online (Sandbox Code Playgroud)
似乎这个调用应该失败:
decltype(foo(int{ 13 })) fail = 42;
cout << fail << endl;
Run Code Online (Sandbox Code Playgroud)
相反它只是打印:
42
它以我可访问的所有编译器的方式工作.这是正确的行为吗?我请求C++标准的引用.
c++ ×10
c++11 ×4
string ×2
templates ×2
algorithm ×1
arrays ×1
c-strings ×1
compilation ×1
constexpr ×1
containers ×1
ctype ×1
decltype ×1
destructor ×1
literals ×1
locale ×1
lvalue ×1
nth-element ×1
result-of ×1
rvalue ×1
selection ×1
sfinae ×1
stdstring ×1
stl ×1
tolower ×1
unions ×1
void ×1