如果所有类/结构数据成员都缺少初始化程序,则可以使用统一初始化语法来构造对象.
struct foo
{
int i;
float f;
};
...
foo bar{ 5, 3.141f };
Run Code Online (Sandbox Code Playgroud)
但是如果一个或多个成员具有初始化器,则统一初始化语法将变为无效.
struct foo
{
int i;
float f = 0;
};
...
foo bar{ 5, 3.141f }; // Compiler error.
Run Code Online (Sandbox Code Playgroud)
我猜测添加数据成员初始值设定项会自动实现一个或多个默认构造函数,并禁止构造函数的默认实现initialization_list.这是预期的标准吗?为什么这样工作?
我尝试使用以下函数来检查android ndk中是否支持std :: async以及windows中的cygwin.我使用的功能如下
机器:64位赢得8与cygwin
Android:r8e
Eclipse:Juno 4.2.1
ADT:22.0.1
struct Foo
{
Foo() : data(0) {}
void sum(int i) { data +=i;}
int data;
};
int main()
{
Foo foo;
auto f = std::async(&Foo::sum, &foo, 42);
f.get();
std::cout << foo.data << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
说明资源路径位置类型无效使用不完整类型'std :: __ async_sfinae_helper :: type {aka struct std :: future}'Sample.cpp/Cli13/jni line 63 C/C++问题
注意:我已经设置了_GLIBCXX_HAS_GTHREADS以及GXX_EXPERIMENTAL_CXX0X以及ATOMIC_INT_LOCK_FREE ...默认情况下,ndk使用4.6工具链.我必须在Properties-> C/C++ - > Includes中手动包含gnu-libstd ++ for 4.7.是否有任何工作使std :: async工作???
谢谢.
下面的代码显示2种溶液(std::to_string和std::stringstream),该转换的int m_currentSoundTime到std::string.是更快std::to_string还是std::stringstream更快?
// Compute current sound time in minute and convert to string
stringstream currentTime;
currentTime << m_currentSoundTime / 60;
m_currentSoundTimeInMinute = currentTime.str();
Run Code Online (Sandbox Code Playgroud)
要么
m_currentSoundTimeInMinute = to_string( m_currentSoundTime / 60 );
Run Code Online (Sandbox Code Playgroud) 有没有办法定义一个只处理xvalue或prvalue的函数(或者这里是一个独占的)?
例如,假设我有一个方法f(foo&& f),如何声明f绑定xvalue类型并拒绝prvalue类型,反之亦然?
struct Foo{};
void xvalue(/*what put here ?*/){}
void prvalue(/*what put here ?*/){}
void main()
{
Foo foo;
prvalue(Foo());//compile
prvalue(std::move(foo)));//fail at compilation
xvalue(std::move(foo)); //compile
xvalue(Foo());//fail at compilation
}
Run Code Online (Sandbox Code Playgroud)
也许通过使用std::enable_if和decltype(e)是xvalues的rvalue引用类型和prvalue的非引用类型这一事实?
我试图对一个真正const的类进行const操作 - 它不会更改类指向的数据.
例如:
class Node{
public:
int val;
};
class V{
public:
Node * node; //what is the change that is needed here?
void const_action()const{
node->val=5; //error wanted here
}
void action(){
node->val=5; //error is not wanted here
}
};
Run Code Online (Sandbox Code Playgroud) 我在cppreference.com上看到了这个例子.我不清楚函数参数的包扩展.
功能参数列表
在函数参数列表中,如果省略号出现在参数声明中(无论它是否命名函数参数包(如,Args ... args))参数声明是模式:
Run Code Online (Sandbox Code Playgroud)template<typename ...Ts> void f(Ts...) {} f('a', 1); // Ts... expands to void f(char, int) f(0.1); // Ts... expands to void f(double) template<typename ...Ts, int... N> void g(Ts (&...)[N]) {} int n[1]; g<const char, int>("a", n); // Ts (&...)[N] expands to const char (&)[2], int(&)[1]注意:在此模式中,省略号是最内层元素,而不是所有其他包扩展中的最后一个元素.
典型的实现方式如下:
template <typename V, typename T>
struct Is_in_tuple;
template <typename V, typename T0, typename... T>
struct Is_in_tuple <V, tuple<T0, T...> >
{
static const bool value = Is_in_tuple<V, tuple<T...> >::value;
};
template <typename V, typename... T>
struct Is_in_tuple <V, tuple<V, T...> >
{
static const bool value = true;
};
template <typename V>
struct Is_in_tuple <V, tuple<> >
{
static const bool value = false;
};
Run Code Online (Sandbox Code Playgroud)
问题出现在VS2012中,其中存在元组,但可变参数模板不存在!
有没有一种解决方法,一种在没有可变参数模板的情况下执行此类测试的方法?
我std::exception_ptr内心有一个例外.我将调用std::rethrow_exception以获取实际异常,异常在catch语句之后是否有效?我的猜测是,因为我仍然坚持std::exception_ptr它仍然有效.
看例子:
std::exception_ptr ePtr = initialize_somewhere_else();
std::runtime_error* a=NULL;
try {
std::rethrow_exception(ePtr);
} catch(std::runtime_error& e) {
a = &e;
}
std::cout << a << ", " << a->what() << std::endl;
Run Code Online (Sandbox Code Playgroud)
注意:在我使用Clang的测试中,这确实有效.
我一直试图写一个特征来判断一些可调用是否将右值引用作为其第一个参数.这使得一些元编程调整在调用可调用区域时是否使用移动或复制语义,其中可调用区域由外部代码提供(实际上一个是在用户提供的可调用类型上过载).
#include <functional>
#include <iostream>
#include <type_traits>
// Does the callable when called with Arg move?
template<class F, class Arg> struct is_callable_moving
{
typedef typename std::decay<Arg>::type arg_type;
typedef typename std::function<F(arg_type)>::argument_type parameter_type;
static constexpr bool value = std::is_rvalue_reference<parameter_type>::value;
};
int main(void)
{
auto normal = [](auto) {}; // Takes an unconstrained input.
auto moving = [](auto&&) {}; // Takes a constrained to rvalue ref input.
std::cout << "normal=" << is_callable_moving<decltype(normal), int>::value << std::endl;
std::cout << "moving=" << is_callable_moving<decltype(moving), int>::value << std::endl; …Run Code Online (Sandbox Code Playgroud) 我有一个constexpr从字符串文字计算CRC32哈希的函数。
template <size_t len>
constexpr uint32_t ctcrc32(const char (&str)[len]) {
return detail::crc32<len - 2>(str) ^ 0xFFFFFFFF;
}
Run Code Online (Sandbox Code Playgroud)
(指其他constexpr功能)
我想做的是调用其他一些接受uint32_t值的函数,并使用它访问某些中的数据unordered_map。这样的调用看起来像这样:
uniformByNameCRC32(ctcrc32("uPointLight.position"));
Run Code Online (Sandbox Code Playgroud)
我希望"uPointLight.position"的哈希值在构建时进行一次计算,然后将结果常量传递给uniformByNameCRC32(),但事实并非如此,并ctcrc32()在运行时被调用,这基本上会杀死CPU,因为我有很多uniformByNameCRC32()调用。
但是,这很好用:
std::array<uint64_t, ctcrc32("string_literal")> array;
Run Code Online (Sandbox Code Playgroud)
这样的代码将编译并指示ctcrc32()的返回值确实是一个constexpr。
我在这里想念什么?