小编xml*_*lmx的帖子

const临时从模板类型和为什么使用std :: add_const?

以下代码摘自cppreference.com.

#include <iostream>
#include <type_traits>

struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
};

template <class T>
void call_m()
{
    T().m();
}

int main()
{
    call_m<foo>();
    call_m<std::add_const<foo>::type>();
}
Run Code Online (Sandbox Code Playgroud)

但是,使用VC++ 2012年11月的CTP编译时,输出为

非CV

非CV

而不是预期的:

非CV

常量

此外,以下两个陈述之间的区别是什么:

call_m<const foo>();

call_m<std::add_const<foo>::type>();

c++ overloading type-traits visual-c++ c++11

9
推荐指数
1
解决办法
472
查看次数

是否保证std :: string_view文字为空终止?

我知道,琐碎小事std::string_view不能保证会以null结尾。但是,我不知道std::string_view文字是否保证为空终止。

例如:

#include <string_view>

using namespace std::literals;

int main()
{
    auto my_sv = "hello"sv;
}
Run Code Online (Sandbox Code Playgroud)

C ++ 17或更高版本是否保证以my_sv.data()Null结尾?

===以下为更新===

以下全部来自n4820

  1. 根据5.13.5.14,字符串文字是空终止的。
  2. 根据5.13.8,用户定义的字符串字面量由字符串文字和自定义后缀组成。说,"hello"svhello是字符串文字,sv是后缀。
  3. 按照5.13.8.5,"hello"sv作为表单的一个呼叫被处理operator "" sv(str, len);为每5.13.5.14,str是空终止。
  4. 按照21.4.2.1,svdata()必须返回str

他们可以证明"hello"sv.data()C ++标准保证它可以为空终止吗?

c++ string standards string-view c++17

9
推荐指数
1
解决办法
384
查看次数

为什么C++允许变量名和类名相同?

#include <iostream>
#include <vector>   

using namespace std; 

typedef int UINT4;

class Hack
{};

Hack& operator <(Hack& a , Hack& b) 
{
    std::cerr << "1";  
    return a; 
}

Hack& operator >(Hack& a, Hack& b)
{
    std::cerr <<  "2";    
    return a; 
}

int main()
{
    Hack vector; // It SHOULD be OK!?
    Hack UINT4; // It SHOULD be OK!?
    Hack v;
    Hack Hack; // It SHOULD be OK!?
    Hack = v; // It SHOULD be OK!?

    // Please stop to think for …
Run Code Online (Sandbox Code Playgroud)

c++ naming-conventions

8
推荐指数
2
解决办法
2331
查看次数

为什么C++ 11的移动构造函数/赋值运算符不按预期运行

#include <iostream>

using namespace std;

struct A
{
    A()
    {
        cout << "A()" << endl;
    }

    ~A()
    {
        cout << "~A()" << endl;
    }

    A(A&&)
    {
        cout << "A(A&&)" << endl;
    }

    A& operator =(A&&)
    {
        cout << "A& operator =(A&&)" << endl;
        return *this;
    }
};

struct B
{
    // According to the C++11, the move ctor/assignment operator
    // should be implicitly declared and defined. The move ctor
    // /assignment operator should implicitly call class A's move
    // ctor/assignment …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ move-semantics c++11 visual-studio-2012

8
推荐指数
2
解决办法
3063
查看次数

如何使用wcout显示L"أبجديةعربية中文"?

我想用wcout显示混有中文的阿拉伯语消息.

以下代码没问题:

#include <iostream>

using namespace std;

int main()
{
    wcout.imbue(locale("chs"));
    wcout << L"??"; // OK
}
Run Code Online (Sandbox Code Playgroud)

但是,以下代码不起作用:

#include <iostream>

using namespace std;

int main()
{
    wcout.imbue(locale(/* What to place here ??? */));
    wcout << L"???????????? ?????????????"; // Output nothing. VC++ 2012 on Win7 x64
    // Why does the main advantage of unicode not apply here?
}
Run Code Online (Sandbox Code Playgroud)

我认为在采用unicode之后应该弃用代码页的概念.

Q1.wout显示这样一个文本的机制是什么?

Q2.为什么Windows作为基于unicode的操作系统不支持在其控制台窗口中输出unicode字符?

c++ unicode io widestring windows-7

8
推荐指数
1
解决办法
2499
查看次数

如何优雅地返回默认初始化的对象?

我有一个类如下:

class VeryVeryVeryLongTypeName
{
    bool is_ok;

    VeryVeryVeryLongTypeName() : is_ok(false) {}
};

VeryVeryVeryLongTypeName f()
{
    VeryVeryVeryLongTypeName v;

    ... // Doing something

    if (condition_1 is true)
    {
        return v;
    }
    else
    {
        return VeryVeryVeryLongTypeName();
    }

    ... // Doing something

    if (condition_2 is true)
    {
        return v;
    }
    else
    {
        return VeryVeryVeryLongTypeName();
    }    
}
Run Code Online (Sandbox Code Playgroud)

我认为这个陈述return VeryVeryVeryLongTypeName();非常乏味和丑陋,所以,我的问题是:

如何优雅地返回默认初始化的对象?

或换句话说:

将功能添加到C++标准中以使以下声明合法是一个好主意吗?

return default; // instead of return VeryVeryVeryLongTypeName();
Run Code Online (Sandbox Code Playgroud)

c++ return default-value default-constructor c++11

8
推荐指数
2
解决办法
401
查看次数

如何将变量模板函数声明为朋友?

如何将变量模板函数声明为朋友?

例如如下:

template<class T>
class A
{
    friend ??? MakeA ??? ; // What should be placed here ???

    A(T)
    {}
};

template<class T, class... Args>
A<T> MakeA(Args&&... args)
{
    T t(std::forward<Args>(args));

    return A(t);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates friend-function variadic-templates c++11

8
推荐指数
1
解决办法
925
查看次数

android的KeyguardManager中isDeviceLocked和isKeyguardSecure有什么区别?

以下摘自http://developer.android.com/reference/android/app/KeyguardManager.html

public boolean isDeviceLocked()

返回设备当前是否已锁定并需要PIN,模式或密码才能解锁.如果解锁设备当前需要PIN,模式或密码,则返回true.

public boolean isKeyguardSecure()

返回键盘锁是否需要密码才能解锁.如果keyguard是安全的,则返回true.

isDeviceLocked和之间有什么区别isKeyguardSecure

api android locking lockscreen keyguard

8
推荐指数
1
解决办法
5473
查看次数

如何移动std :: ostringstream的底层字符串对象?

#include <sstream>
#include <string>

using namespace std;

template<typename T>
string ToString(const T& obj)
{
    ostringstream oss;
    oss << obj;

    //
    // oss will never be used again, so I should
    // MOVE its underlying string.
    //
    // However, below will COPY, rather than MOVE, 
    // oss' underlying string object!
    //
    return oss.str();
}
Run Code Online (Sandbox Code Playgroud)

如何移动std :: ostringstream的底层字符串对象?

c++ performance rvalue-reference move-semantics c++11

8
推荐指数
1
解决办法
379
查看次数

什么类型会使"std :: has_unique_object_representations"返回false?

cppref,我看到一个奇怪的类型特征检查器:std::has_unique_object_representations

从它的描述,我无法想象任何类型T,使std::has_unique_object_representations<T>::valuefalse.

有没有反例?

c++ standards type-traits c++17

8
推荐指数
1
解决办法
547
查看次数