标准C++中有算法std::count/ std::count_if.
template<class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count(InputIterator first, InputIterator last, const T& value);
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred);
Run Code Online (Sandbox Code Playgroud)
效果:返回以下相应条件所适用的[first,last]范围内的迭代器数量:*i == value,pred(*i)!= false.
difference_type是iterator's difference_type,可以是负数,但count只能返回值> = 0.为什么difference_type而不是size_t例如?
在Visual Studio 2012RC中,有一些非标准扩展.例如,此代码编译:
#include <string>
using namespace std;
void value(string& value)
{
value = "some";
}
int main()
{
value(string("nice"));
}
Run Code Online (Sandbox Code Playgroud)
并警告它是非标准扩展名.所以,我想了解它是如何实现的以及代码如何转换(使用const_cast进行rvalue-reference或const引用)?
首先,请查看以下代码,其中包含2个翻译单元.
--- foo.h ---
class Foo
{
public:
Foo();
Foo(const Foo& rhs);
void print() const;
private:
std::string str_;
};
Foo getFoo();
--- foo.cpp ---
#include <iostream>
Foo::Foo() : str_("hello")
{
std::cout << "Default Ctor" << std::endl;
}
Foo::Foo(const Foo& rhs) : str_(rhs.str_)
{
std::cout << "Copy Ctor" << std::endl;
}
void Foo:print() const
{
std::cout << "print [" << str_ << "]" << std:endl;
}
Foo getFoo()
{
return Foo(); // Expecting RVO
}
--- main.cpp ---
#include "foo.h"
int …Run Code Online (Sandbox Code Playgroud) 我有以下片段
#include <type_traits>
#include <boost/type_traits.hpp>
class C { C() { } };
int main()
{
static_assert(!boost::has_trivial_default_constructor<C>::value, "Constructible");
static_assert(!std::is_default_constructible<C>::value, "Constructible");
}
Run Code Online (Sandbox Code Playgroud)
条件不相等,但第一个条件工作正常,第二个构造给出错误,构造函数是私有的.编译器gcc 4.7 ...那么,这个gcc错误,还是由标准定义?
http://liveworkspace.org/code/NDQyMD $ 5
好.由于这种情况确实不平等 - 我们可以使用这样的东西
#include <type_traits>
#include <boost/type_traits.hpp>
class C { private: C() noexcept(false) { } };
int main()
{
static_assert(!boost::has_nothrow_constructor<C>::value, "Constructible");
static_assert(!std::is_nothrow_constructible<C>::value, "Constructible");
}
Run Code Online (Sandbox Code Playgroud)
http://liveworkspace.org/code/NDQyMD $ 24
无论如何,我知道,static_assert应该不会失败,因为类型实际上不是默认的可构造/不可构造的.问题是:为什么存在编译错误,而不是我的静态断言?
示例代码
#include <iostream>
struct base {};
template<typename Type>
struct left : base {
Type value;
};
template<typename Type>
struct right : base {
Type value;
};
int main() {
std::cout << "sizeof left<base> = " << sizeof(left<base>) << std::endl;
std::cout << "sizeof left<right<base>> = " << sizeof(left<right<base>>) << std::endl;
std::cout << "sizeof left<right<left<right<left<base>>>>> = " << sizeof(left<right<left<right<left<base>>>>>) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
产量
GCC 4.6是
sizeof left<base> = 2
sizeof left<right<base>> = 3
sizeof left<right<left<right<left<base>>>>> = 6
Run Code Online (Sandbox Code Playgroud)
用clang 3.1
sizeof left<base> = …Run Code Online (Sandbox Code Playgroud) 引自n3337 12.3.1/3
非显式复制/移动构造函数(12.8)是转换构造函数.隐式声明的复制/移动构造函数不是显式构造函数; 可以调用它来进行隐式类型转换.
引自ANSI ISO IEC 14882 2003
非显式复制构造函数(12.8)是转换构造函数.隐式声明的复制构造函数不是显式构造函数; 可以调用它来进行隐式类型转换.
我没有想法,怎么copy-constructor可以用于implicit类型conversions.如果它是标准中的错误打印/错误,为什么它自C++ 03标准以来没有得到纠正?任何链接和示例(如果我们可以使用它type conversions)都非常感谢.
我们有以下代码
int main()
{
void f() throw(int);
f();
return 0;
}
void f() { }
Run Code Online (Sandbox Code Playgroud)
GCC和clang汇编得很好.但是,在标准中有这样的段落:
n3376 15.4/4
如果函数的任何声明具有异常规范,该规范不是允许所有异常的noexcept 规范,则该函数的所有声明(包括定义和任何显式特化)都应具有兼容的异常规范.
以下示例:gcc - 错误,clang - 警告
void f() throw(int);
int main()
{
f();
return 0;
}
void f() { }
Run Code Online (Sandbox Code Playgroud)
为什么这些片段有区别?谢谢.
在回答完这个问题并阅读本文后,看看这段代码,我想constexpr find用简单的数组类实现.
考虑以下示例:
#include <cstddef>
template <class It, class T>
constexpr auto constexpr_find(const It& b, const It& e, T value) {
auto begin = b;
while (begin != e) {
if (*begin == value) break;
++begin;
}
return *begin;
}
template<typename T, size_t N>
class array
{
public:
typedef T* iterator;
typedef const T* const_iterator;
constexpr auto begin() const { return const_iterator(array_); }
constexpr auto end() const { return const_iterator(array_ + N); …Run Code Online (Sandbox Code Playgroud) 我们有以下代码(它更复杂,我试着做一个最小的例子)。
#include <iostream>
#include <vector>
#include <string>
#include <memory>
template<typename T>
struct use_type
{
use_type(T& v) : value(v) {}
T& value;
};
template<typename T>
use_type<T> use(T& value) { return use_type<T>(value); }
template<typename T>
use_type<T> use(const T& value) { return use_type<T>(const_cast<T&>(value)); }
template<typename T>
struct printer_helper
{
void use(const use_type<T>& use) { uses.push_back(use); }
void final_action()
{
for (const auto& use : uses)
{
std::cout << use.value.size() << std::endl;
for (const auto& v : use.value)
{
std::cout << v << ","; …Run Code Online (Sandbox Code Playgroud) 所以我有类似的东西:
template<int X>
class foo {
char a[X];
...
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类'bar',其中包含如下函数:
void execute(foo &b);
Run Code Online (Sandbox Code Playgroud)
哪个应该在foo中的char数组上执行任务但是它给我一个错误,说它是一个模板类但是使用类似的东西:
void execute(foo<int> &b);
Run Code Online (Sandbox Code Playgroud)
也给出了错误.我不确定如何传递它作为唯一没有给我错误的是如果我静态给它一个值,如:
void execute(foo<4> &b);
Run Code Online (Sandbox Code Playgroud)
非常感谢!