根据这里,explicit:
指定不允许隐式转换或复制初始化的构造函数和转换运算符(自C++ 11开始).
那么,这两种技术是否相同?
struct Z {
// ...
Z(long long); // can initialize with a long long
Z(long) = delete; // but not anything smaller
};
struct Z {
// ...
explicit Z(long long); // can initialize ONLY with a long long
};
Run Code Online (Sandbox Code Playgroud) 实际上我的问题都在标题中.
无论如何:
我有一个类,我使用显式构造函数:
.h
class MyClass
{
public:
explicit MyClass(const string& s): query(s) {}
private:
string query;
}
Run Code Online (Sandbox Code Playgroud)
是否必须在实现(.cpp)文件中放置显式关键字?
int value = 5; // this type of assignment is called an explicit assignment
int value(5); // this type of assignment is called an implicit assignment
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别(如果有的话),以及在什么情况下显式和隐式赋值有何不同以及如何?
http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx
编辑:我实际上刚刚发现这篇文章,它使整个事情变得更加清晰......并且它提出了另一个问题,如果你(通常)标记构造函数采用基本类型的单个参数 - numeric/bool/string -作为明确的,并保留其余的(当然要注意像构造函数这样的陷阱(int, SomeType = SomeType())?
它只是偏好还是有特定的情况,其中一个是必要的而不是另一个?我正在参考以下变体进行初始化
T t(e); // direct initialization
T t = e; // copy initialization
Run Code Online (Sandbox Code Playgroud) c++ explicit-constructor implicit-conversion copy-initialization
由于std::unique_ptr提供了一种避免内存泄漏并确保异常安全的便捷方法,因此传递它们而不是原始指针是明智的.因此,人们可能想要具有签名的(成员)函数
std::unique_ptr<some_type> foo(some data);
Run Code Online (Sandbox Code Playgroud)
不幸的是,在实现这样的功能时,不能简单地
std::unique_ptr<some_type> foo(some data)
{
return { new some_type(data) }; // error
}
Run Code Online (Sandbox Code Playgroud)
但必须改为
std::unique_ptr<some_type> foo(some data)
{
return std::move( std::unique_ptr<some_type>( new some_type(data) ) ); // awkward
}
Run Code Online (Sandbox Code Playgroud)
因为构造函数unique_ptr::unique_ptr(pointer)是explicit.这个构造函数背后的原因是explicit什么?
构造函数的一个动机explicit是防止意外的隐式类型转换.但是,由于unique_ptr不能通过价值传递,这应该不是一个真正的问题,是吗?
在此演示文稿中:http://qtconference.kdab.com/sites/default/files/slides/mutz-dd-speed-up-your-qt-5-programs-using-c++11.pdf
作者建议N-ary构造函数受益于C++ 11版本的explicit关键字.
如果你有多个构造函数参数,那么在C++ 11中有什么变化可以使这个关键字变得有用?
当使用using继承基类的构造函数时,英特尔C++编译器(版本16.0.3.207 Build 20160415)似乎删除了显式说明符.这是一个错误吗?
struct B
{
explicit B(int) { }
};
struct D : B
{
using B::B;
};
B b = 1; // Not OK, fine
D d = 1; // Not OK with Microsoft C++ and GCC, but OK with Intel C++
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,编译器无法确定我想要使用哪个构造函数.为什么,我该如何解决这个问题?(实例)
#include <tuple>
#include <functional>
#include <iostream>
template<typename data_type, typename eval_type, typename Type1, typename Type2>
class A
{
public:
using a_type = std::tuple<Type1, Type2>;
using b_type = std::tuple<std::size_t,std::size_t>;
inline explicit constexpr A(const std::function<data_type(a_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "idx_type" << std::endl;
}
inline explicit constexpr A(const std::function<data_type(b_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "point_type" << std::endl;
}
};
int main()
{
int a …Run Code Online (Sandbox Code Playgroud) 标准库类模板std::bitset<N>有一个构造函数(C++ 11及更高版本,unsigned longC++ 11之前的参数)
constexpr bitset(unsigned long long) noexcept
Run Code Online (Sandbox Code Playgroud)
与许多最佳实践指南相反,此单参数构造函数未标记为explicit.这背后的理由是什么?
c++ explicit-constructor unsigned-long-long-int bitset implicit-conversion
我们使用的外部库包含以下显式构造函数:
class Chart {
public:
explicit Chart(Chart::Type type, Object *parent);
// ...
};
Run Code Online (Sandbox Code Playgroud)
编译器抱怨以下警告:
chart.h: warning #2305: declaration of 'explicit' constructor
without a single argument is redundant
Run Code Online (Sandbox Code Playgroud)
是否二进制兼容只是删除explicitchart.h 中的关键字而不重新编译库以避免警告?我的感觉是它很安全,因为explicit在这种情况下无论如何都没有意义.谁能确认一下?