counter 是一个 int
void SentryManager::add(std::string name,std::shared_ptr<Sentry>){
name = name + std::to_string(counter);
}
Run Code Online (Sandbox Code Playgroud)
什么是阻止此错误的最佳方法?当我懒惰时,我只是制作了int long long(或其他东西),但我确信有更好的解决方法.
错误信息:
sentrymanager.cpp(8): error C2668: 'std::to_string' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual C++ 2010 Express.
有没有办法override在Visual C++ 2012中强制使用C++ 11 关键字?
(即如果我忘记说override,那么我想得到一个警告/错误.)
正如Boost中的情况一样,C++ 11提供了一些用于转换的函数shared_ptr:
std::static_pointer_cast
std::dynamic_pointer_cast
std::const_pointer_cast
Run Code Online (Sandbox Code Playgroud)
但是,我想知道为什么没有等效功能unique_ptr.
考虑以下简单示例:
class A { virtual ~A(); ... }
class B : public A { ... }
unique_ptr<A> pA(new B(...));
unique_ptr<A> qA = std::move(pA); // This is legal since there is no casting
unique_ptr<B> pB = std::move(pA); // This is not legal
// I would like to do something like:
// (Of course, it is not valid, but that would be the idea)
unique_ptr<B> pB = std::move(std::dynamic_pointer_cast<B>(pA));
Run Code Online (Sandbox Code Playgroud)
是否有任何理由不鼓励这种使用模式,因此,shared_ptr没有提供与现有模式相同的功能unique_ptr …
考虑以下程序:
#include <array>
int main()
{
std::array<int, 1> x = { 0 }; // warning!
x = { { 0 } }; // no warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一次初始化导致gcc 4.7.2上的警告......
main.cpp:5:22: warning: unused variable ‘x’ [-Wunused-variable]
Run Code Online (Sandbox Code Playgroud)
......和铿锵3.1
main.cpp:5:28: warning: suggest braces around initialization of subobject [-Wmissing-braces]
std::array<int, 1> x = { 0 };
Run Code Online (Sandbox Code Playgroud)
就标准而言,双花键或单花括号之间应该没有区别,至少在这个例子中.
有两种方法可以处理警告:
你有什么建议?恕我直言,双卷曲表达看起来有些难看.另一方面,警告可能会在更复杂的示例中检测到实际问题.你知道这个警告对你有帮助的例子吗?
编辑,为了避免混淆:
decltype不不接受两个参数.看到答案.
以下两个结构可用于T在编译期间检查类型上成员函数的存在性:
// Non-templated helper struct:
struct _test_has_foo {
template<class T>
static auto test(T* p) -> decltype(p->foo(), std::true_type());
template<class>
static auto test(...) -> std::false_type;
};
// Templated actual struct:
template<class T>
struct has_foo : decltype(_test_has_foo::test<T>(0))
{};
Run Code Online (Sandbox Code Playgroud)
我认为这个想法是在检查成员函数的存在时使用SFINAE,因此如果p->foo()无效,则只test返回返回的省略号版本std::false_type.否则,第一个方法被定义T*并将返回std::true_type.实际的"切换"发生在第二个类中,它继承自返回的类型test.与不同的方法相比,这看起来更聪明,更"轻巧" is_same.
在decltype与两个参数第一次看令我感到诧异,因为我认为这只是得到一个表达式的类型.当我看到上面的代码时,我认为它类似于"尝试编译表达式并始终返回第二种类型.如果表达式无法编译则失败"(所以隐藏此专业化; SFINAE).
但:
然后我想我可以使用这个方法来编写任何"is valid expression"检查器,只要它依赖于某种类型T.例:
...
template<class T>
static auto test(T* p) -> decltype(bar(*p), std::true_type());
...
Run Code Online (Sandbox Code Playgroud)
这一点,所以我想,将返回 …
是否有任何现有的迭代器实现(可能在boost中)实现某种展平迭代器?
例如:
unordered_set<vector<int> > s;
s.insert(vector<int>());
s.insert({1,2,3,4,5});
s.insert({6,7,8});
s.insert({9,10,11,12});
flattening_iterator<unordered_set<vector<int> >::iterator> it( ... ), end( ... );
for(; it != end; ++it)
{
cout << *it << endl;
}
//would print the numbers 1 through 12
Run Code Online (Sandbox Code Playgroud) C++ 1z将引入"constexpr if" - 如果将根据条件删除其中一个分支.似乎合理有用.
但是,没有constexpr关键字是不可能的?我认为在编译期间,编译器应该知道编译时间是否已知.如果是,即使是最基本的优化级别也应该删除不必要的分支.
例如(参见godbolt:https://godbolt.org/g/IpY5y5 ):
int test() {
const bool condition = true;
if (condition) {
return 0;
} else {
// optimized out even without "constexpr if"
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
Godbolt探险家表示,即使是带有-O0的gcc-4.4.7也没有编译"返回1",所以它实现了constexpr所承诺的.显然,当条件是constexpr函数的结果时,这样的旧编译器将无法这样做,但事实仍然存在:现代编译器知道条件是否为constexpr,并且不需要我明确地告诉它.
所以问题是:
为什么"constexpr if"需要"constexpr"?
我正在学习C++,我无法告诉我何时需要使用它::.我知道我需要使用std::前面cout和cin.这是否意味着的是内部iostream文件创建它做了一个叫做命名空间的开发std,并把功能cin和cout进入所谓的命名空间std?当我创建一个不属于同一文件的新类时,main()我必须添加::.
例如,如果我创建一个class被调用的A,为什么我需要放在我创建的A::函数前面,即使我没有把它放到名字中?例如void A::printStuff(){}.如果我在中创建一个函数main,为什么我不必放main::printStuf{}?
我知道我的问题可能令人困惑,但有人可以帮助我吗?
我遇到了问题std::max.我无法弄清楚.
int border = 35;
int myInt = 2;
int myOtherInt = 3;
int z = std::max(myInt + 2 * border, myOtherInt + 2 * border);
Run Code Online (Sandbox Code Playgroud)
我已经包含了算法标准头.当我鼠标移动到最大值时,我得到:
错误:期望标识符
并编译错误:
错误C2589 ::错误C2059
'('右侧的非法令牌'::'
:语法错误:'::'
怎么了?
如何使用C++ 11创建计时器事件?
我需要这样的话:"从现在起1秒钟后给我打电话".
有图书馆吗?
c++ ×10
c++11 ×7
visual-c++ ×2
c++17 ×1
casting ×1
constexpr ×1
decltype ×1
function ×1
if-constexpr ×1
iterator ×1
namespaces ×1
overriding ×1
sfinae ×1
std ×1
type-traits ×1
unique-ptr ×1