#include <codecvt>
#include <string>
#include <locale>
std::string to_gbk(const std::wstring& u16_str)
{
using Facet = std::codecvt_byname<wchar_t, char, std::mbstate_t>;
std::wstring_convert
<std::codecvt<wchar_t, char, std::mbstate_t>>
wstr_2_gbk(new Facet("zh_CN.GBK"));
return wstr_2_gbk.to_bytes(u16_str);
}
int main()
{
to_gbk(L"");
}
Run Code Online (Sandbox Code Playgroud)
clang和vc ++都可以,但是gcc 6.2输出:
[root@localhost ~]# g++ main.cpp
In file included from /usr/include/c++/6.2.1/bits/locale_conv.h:41:0,
from /usr/include/c++/6.2.1/locale:43,
from main.cpp:3: /usr/include/c++/6.2.1/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>]’:
/usr/include/c++/6.2.1/bits/unique_ptr.h:236:17: required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>; _Dp = std::default_delete<std::codecvt<wchar_t, char, __mbstate_t> >]’
/usr/include/c++/6.2.1/bits/locale_conv.h:218:7: required from …Run Code Online (Sandbox Code Playgroud) 下面的代码clang 3.8.1-1在ArchLinux上正确编译.
这个clang错误吗?
gcc 在此发出正确的警告/错误.
template <class T>
struct BugReproducer{
using size_type = typename T::size_type;
int bug1(size_type count);
int bug2(size_type count) const;
static int bug3(size_type count);
};
template <class T>
int BugReproducer<T>::bug1(size_type const count){
// this is a bug. must be not allowed
count = 5;
// return is to use the result...
return count;
}
template <class T>
int BugReproducer<T>::bug2(size_type const count) const{
// same for const method
count = 5;
return count;
}
template …Run Code Online (Sandbox Code Playgroud) 我一直在试图理解何时何时不具有捕获默认odr的lambda-使用在其周围范围内定义的自动存储持续时间的变量(由此答案提示).在探索周围时,我偶然发现了一点点好奇心.GCC和Clang似乎不同意n以下代码中id-expression的值类别:
template <typename T> void assert_is_lvalue(const T&) {}
template <typename T> void assert_is_lvalue(const T&&) = delete;
int main() {
const int n = 0;
[=] { assert_is_lvalue(n); };
}
Run Code Online (Sandbox Code Playgroud)
Clang成功编译代码,而GCC不编译(error: use of deleted function).哪一个是正确的?或者这是未指定或实现定义的东西?
绑定对象的引用应该使用它,并且通过删除lambda的capture-default并观察两个编译器然后抱怨在n没有捕获默认值的情况下不能隐式捕获来确认.
将lambda标记为mutable编译器的输出没有明显的差别.
我正在尝试理解在主线程的上下文中具有静态存储持续时间和线程局部存储持续时间的命名空间范围和块范围对象的初始化和销毁的排序规则.考虑这两个类:
struct Foo {
Foo() { std::cout << "Foo\n"; }
~Foo() { std::cout << "~Foo\n"; }
static Foo &instance();
};
struct Bar {
Bar() { std::cout << "Bar\n"; }
~Bar() { std::cout << "~Bar\n"; }
static Bar &instance();
};
Run Code Online (Sandbox Code Playgroud)
除了静态instance成员函数的实现之外,它们是相同的:
thread_local Foo t_foo;
Foo &Foo::instance() { return t_foo; }
Bar &Bar::instance() { static Bar s_bar; return s_bar; }
Run Code Online (Sandbox Code Playgroud)
Bar 是一个Meyers单例,一个具有静态存储持续时间的块范围对象.
Foo的实例是具有线程本地存储持续时间的命名空间范围对象.
现在main功能:
int main() {
Bar::instance();
Foo::instance();
}
Run Code Online (Sandbox Code Playgroud)
这是GCC 8.1.0和Clang 5.0.0的输出:
Bar
Foo
~Foo
~Bar …Run Code Online (Sandbox Code Playgroud) 我有一个OpenGL项目的问题,从void*指针转换为shared_ptr<mytype>.
我正在使用Bullet在刚体上设置指针:
root_physics->rigidBody->setUserPointer(&this->root_directory->handle);
Run Code Online (Sandbox Code Playgroud)
手柄属于类型shared_ptr<mytype>.
该void*指针由子弹的库函数返回,getUserPointer():
RayCallback.m_collisionObject->getUserPointer()
Run Code Online (Sandbox Code Playgroud)
要转换回mytype,static_cast不起作用:
std::shared_ptr<disk_node> u_poi = static_cast< std::shared_ptr<disk_node> >( RayCallback.m_collisionObject->getUserPointer() );
Run Code Online (Sandbox Code Playgroud)
错误,在编译时:
/usr/include/c++/4.8/bits/shared_ptr_base.h:739:39: error: invalid conversion from ‘void*’ to ‘mytype*’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
知道如何从void*返回的转换getUserPointer()为shared_ptr<mytype>?
我有一些代码需要我将一些数据放入std::array.我想我可以通过交换两个阵列并丢弃其中一个来做到这一点.这是代码
int main()
{
std::array<double, 10> a;
std::array<double, 5> b;
/*populate b*/
/*swap them round*/
std::swap(a, b);
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个非常奇怪的编译器错误(MSVC2013).
CashFlows.cpp(27): error C2665: 'std::swap' : none of the 3 overloads could convert all the argument types
include\exception(502): could be 'void std::swap(std::exception_ptr &,std::exception_ptr &)'
include\tuple(572): or 'void std::swap(std::tuple<> &,std::tuple<> &)'
include\thread(232): or 'void std::swap(std::thread &,std::thread &) throw()'
while trying to match the argument list '(std::array<_Ty,_Size>, std::array<_Ty,_Size>)'
with
[
_Ty=double,
_Size=0x0a
]
and
[
_Ty=double,
_Size=0x05
]
Run Code Online (Sandbox Code Playgroud)
我不明白.是什么std::tuple<>等有什么关系呢?
在模板类中,我试图使用dynamic_cast从文件中读取字符串,并希望能够使用bad_cast异常捕获失败的强制转换.但是,在编译时(将测试程序设置为double作为模板类,我得到了dynamic_cast的这个错误:
datafilereader.cpp(20): error C2680: 'double *' : invalid target type for dynamic_cast
Run Code Online (Sandbox Code Playgroud)
我试着把它写成<T>而不是<T*>(当我看到关于动态演员的其他问题时,后者似乎是常见的方式......),但实际上是同样的错误.
DataFileReader.cpp
#include "DataFileReader.h"
#include <typeinfo>
template <typename T>
void DataFileReader<T>::openFiles() {
dataFileStream.open(dataFileName);
errorFileStream.open(errorFileName, ios::app);
if (!(dataFileStream.is_open() && errorFileStream.is_open()))
throw(runtime_error("Couldn't open at least one of the files."));
}
template <typename T>
bool DataFileReader<T>::readNextValue(T &aValue) {
ios_base::iostate mask = ios::eofbit|ios::failbit|ios::badbit;
dataFileStream.exceptions(mask);
while (true) {
string readValue;
try {
dataFileStream >> readValue;
aValue = dynamic_cast<T*>(readValue);
return true;
}
catch(bad_cast &bc) {
errorFileStream << readValue << " …Run Code Online (Sandbox Code Playgroud) 也许有很好的解决方案适用于g ++ 4.6.{3,4}?您可以登录https://godbolt.org/
#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};
template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}
int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}
Run Code Online (Sandbox Code Playgroud)
g ++ /tmp/enable_if.cpp -std = c ++ 0x
/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> …Run Code Online (Sandbox Code Playgroud)