我刚刚升级到Mountain Lion,所以我可以在xcode附带的新版Clang上使用一些C++ 11特性.我正在使用Homebrew的cmake 2.8.9.
我做了一个非常简单的CMake项目,它为C++ 11添加了编译器标志:
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_executable(test test.cxx)
add_definitions(-std=c++0x -stdlib=libc++)
Run Code Online (Sandbox Code Playgroud)
test.cxx中的C++代码如下:
#include <iostream>
int main()
{
std::cout << "Howdy" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行cmake和make时,文件编译得很好,但链接器输出以下错误:
Linking CXX executable test
Undefined symbols for architecture x86_64:
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
"std::__1::ios_base::getloc() const", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in …Run Code Online (Sandbox Code Playgroud) 我最近安装了VS2012.在VS2010下编译好的C++项目(.Net 4.0)HashSet<T>在VS2012上无法识别.我甚至试图明确遵循以下声明:
System::Collections::Generic::HashSet< String^ >^ _reasons;
Run Code Online (Sandbox Code Playgroud)
但这只会导致错误:
error C2039: 'HashSet' : is not a member of 'System::Collections::Generic
Run Code Online (Sandbox Code Playgroud)
文档说它在System.Collections.Generic中.C++编译器并不这么认为.
关于去哪里的任何想法?
许多标准库算法都采用谓词函数.但是,这些谓词的类型是用户提供的任意模板参数.为什么C++ 11没有指定这些类型采用特定类型std::function?例如:
template< class InputIt >
InputIt find_if( InputIt first, InputIt last,
std::function<bool()> p );
Run Code Online (Sandbox Code Playgroud)
是不是使用这个而不是模板作为参数类型不是更干净?
我的核心问题是,当使用Qt Creator作为"通用"(非Qt)项目的代码编辑器时,如何告诉它使用c ++ 11语法高亮?
我有一个c ++ 11项目,我已经工作了一段时间,我决定尝试给Qt Creator.这是一个简单的vanilla c ++项目,带有手动编码的makefile等等.
Qt Creator打开项目("eSLIME")就好了,创建了三个文件:eSLIME.config,eSLIME.includes和eSLIME.files.它没有创建.pro文件.
似乎没有认识到c ++ 11的调用.例如,它#include <unordered_set>以绿色突出显示" ",表示没有此类文件或目录.
我怀疑我应该在.config文件中添加一些东西,但我无法弄清楚什么和谷歌搜索没有帮助.我试着附加-std = c ++ 0x,但是没有用.
PS:代码太破碎了,无法立即构建,这就是我切换到IDE的原因.
C++ 11提供了std::allocator_traits类作为使用分配器的标准方法.static函数std::allocator_traits::construct()接受指向对象构造位置的指针.的std::allocator_traits::allocate()静态功能,但是,返回一个allocator::pointer值,该值仅必须表现得像一个指针,但它不一定是一个(通常,虽然std::allocator::pointer需要是一个指针).
一般来说,如果它们可以使用不兼容的类型,那么应该如何使用分配和构造静态方法?只有当pointer类型实际上可以转换为普通的普通指针时才可以使用它们吗?
有人可以向我解释为什么这不能编译:
#include <iterator>
#include <iostream>
#include <unordered_set>
#include <utility>
#include <set>
template<typename T>
std::unordered_set<T> FailMove(std::set<T> &&set) {
std::unordered_set<T> response;
response.insert(std::make_move_iterator(set.begin()),
std::make_move_iterator(set.end()));
return response;
}
int main(int argc, char **argv) {
std::set<int> set{1, 3, 5, 7};
auto res = FailMove(std::move(set));
std::cout << res.size() << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
clang输出(命令:) clang++ -std=c++11 -otest test.cpp是:
In file included from test.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:948:14: error: cannot
cast from lvalue of type 'const value_type' (aka 'const int') to rvalue reference type 'reference' (aka 'int &&'); types …Run Code Online (Sandbox Code Playgroud) 所以我有一个提供字符串类型的现有库.
它隐式转换为C样式字符串,如下所示:
struct TypeIDoNotOwn {
TypeIDoNotOwn() {}
TypeIDoNotOwn(TypeIDoNotOwn const&) {}
TypeIDoNotOwn(char const*) {}
TypeIDoNotOwn& operator=(TypeIDoNotOwn const&) {return *this;}
TypeIDoNotOwn& operator=(char const*) {return *this;}
operator char const*() const {return nullptr;}
};
Run Code Online (Sandbox Code Playgroud)
它有其他方法,但我不认为它们很重要.这些方法有机构,但我的问题不涉及它们,所以我把它们剔除了.
我想要做的是创建一个可以与上述类型相对可互换使用的新类型,并使用"raw string constants".我希望能够获取一个实例TypeIDoNotOwn,并替换它TypeIDoOwn,并编译代码.
举个例子,这套操作:
void test( TypeIDoNotOwn const& x ) {}
int main() {
TypeIOwn a = TypeIDoNotOwn();
TypeIDoNotOwn b;
a = b;
b = a;
TypeIOwn c = "hello";
TypeIDoNotOwn d = c;
a = "world";
d = "world";
char const* e …Run Code Online (Sandbox Code Playgroud) 给定一个数组a,我想countof(a)将数组中的元素数作为编译时常量.如果我有一个指针p,我想countof(p)不编译.这似乎应该是(1)直截了当,(2)通常涵盖在SO中,但(1)我无法使它工作,并且(2)搜索SO没有发现任何东西.
这是我的尝试.
#include <cstddef>
#include <type_traits>
template<typename T, std::size_t n,
typename = typename std::enable_if<std::is_array<T>::value>::type>
constexpr std::size_t countof(T (&)[n]) { return n; }
template<typename T,
typename = typename std::enable_if<std::is_pointer<T>::value>::type>
void countof(T*) = delete;
int main()
{
int a[10];
auto asize = countof(a); // should compile
static_assert(countof(a) == 10,
"countof(a) != 10!");
int *p;
auto psize = countof(p); // shouldn't compile
}
Run Code Online (Sandbox Code Playgroud)
救命?
我不擅长编程,最近开始阅读有关C++的教程.
我决定尝试制作一个简单的二十一点程序.我试图用"大文本"制作一个标题,但C++阻止我这样做,因为它正在检测文本中的其他内容.
//Start Screen Begin
cout << " ____ _ _ _ _ ";
cout << "| __ )| | __ _ ___| | __(_) __ _ ___| | __ ";
cout << "| _ \| |/ _` |/ __| |/ /| |/ _` |/ __| |/ / ";
cout << "| |_) | | (_| | (__| < | | (_| | (__| < ";
cout << "|____/|_|\__,_|\___|_|\_\/ |\__,_|\___|_|\_\ ";
cout << " |__/ ";
//Start Screen End
Run Code Online (Sandbox Code Playgroud)
这是我试图显示,但不断收到以下错误: …
如果我们有
std::experimental::optional<int> x;
Run Code Online (Sandbox Code Playgroud)
以下两行都没有编译:
std::experimental::optional<unsigned int> y; y = x;
std::experimental::optional<unsigned int> z(x);
Run Code Online (Sandbox Code Playgroud)
......即使在我看来是有道理的,就像一个分配int到unsigned int.为什么不能这样做?也就是说,库没有为这种情况实现复制ctor和赋值运算符而避免的缺陷是什么?