有没有办法定义从用户定义的类到基本类型(int,short等)的类型转换?此外,任何此类机制是否需要显式转换,还是隐式工作?
例如:
// simplified example class
class MyNumberClass
{
private:
int value;
public:
// allows for implicit type casting/promotion from int to MyNumberClass
MyNumberClass(const int &v)
{
value = v;
}
};
Run Code Online (Sandbox Code Playgroud)
// defined already above
MyNumberClass t = 5;
// What's the method definition required to overload this?
int b = t; // implicit cast, b=5.
// What's the method definition required to overload this?
int c = (int) t; // C-style explicit cast, c=5.
// ... etc. for …Run Code Online (Sandbox Code Playgroud) 这与Matthieu M.提供的关于如何利用+运算符重载(通常是不直接重新分配回左侧参数的运算符)一起使用移动语义的答案有关.
他建议实施三种不同的重载:
inline T operator+(T left, T const& right) { left += right; return left; }
inline T operator+(T const& left, T right) { right += left; return right; } // commutative
inline T operator+(T left, T&& right) { left += right; return left; } // disambiguation
Run Code Online (Sandbox Code Playgroud)
1号和3号是有道理的,但我不明白2的目的是什么.评论建议交换处理,但似乎1和2是互斥的(即实现两个结果模糊)
例如,所有3个实现:
T a, b, c;
c = a + b;
Run Code Online (Sandbox Code Playgroud)
编译器输出:
1> error C2593: 'operator +' is ambiguous 1> could be 'T operator +(const T &,T)' 1> or 'T …
我最近下载了TBB41_20130613(此时Windows的当前版本),我注意到有文件夹vc11和vc11_uibin/lib文件夹.据我所知,两者都有相同的库(文件名相同),但它们显然是不同的二进制文件(不同的文件大小).我没有在文档中找到任何参考,也没有通过Google找到两者之间的区别.
这两者有什么区别?我什么时候应该使用另一个,还是我必须同时引用它们?
有没有办法对数据数组执行局部排序,以便对最后n个元素进行排序?好吧我的意思是使用标准库,而不是实现我自己的排序功能(这就是我现在正在做的事情).示例输出(使用较少的比较器):
2 1 4 || 5 6 8 10
后面||的元素都比元素之前的元素大||,但只保证对元素右侧的元素||(更靠近数组末尾的索引)进行排序.
这基本上是std :: partial_sort函数的反转,它对左(第一)元素进行排序.
我在使NumPy C API正确初始化时遇到问题.我想我已经把问题import_array从一个不同的翻译单元中调出来了,但我不知道为什么这个问题很重要.
最小的工作示例:
header1.hpp
#ifndef HEADER1_HPP
#define HEADER1_HPP
#include <Python.h>
#include <numpy/npy_3kcompat.h>
#include <numpy/arrayobject.h>
void initialize();
#endif
Run Code Online (Sandbox Code Playgroud)
file1.cpp
#include "header1.hpp"
void* wrap_import_array()
{
import_array();
return (void*) 1;
}
void initialize()
{
wrap_import_array();
}
Run Code Online (Sandbox Code Playgroud)
file2.cpp
#include "header1.hpp"
#include <iostream>
void* loc_wrap_import_array()
{
import_array();
return (void*) 1;
}
void loc_initialize()
{
loc_wrap_import_array();
}
int main()
{
Py_Initialize();
#ifdef USE_LOC_INIT
loc_initialize();
#else
initialize();
#endif
npy_intp dims[] = {5};
std::cout << "creating descr" << std::endl;
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_FLOAT64);
std::cout << …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个允许用户输入某种类型的开始和结束迭代器的函数,然后所有函数都对数据执行一些操作.然而,该功能必须足够通用的,应该对很多类型的迭代工作(例如std::vector::iterator,std::string::iterator,std::iterator等).唯一的限制是迭代器必须至少是forward_iterator_tag功能.
我的函数原型看起来像这样:
template <class key_type, class data_type> std::shared_ptr<data_type>
remove(std::iterator<std::forward_iterator_tag, key_type> key_start,
std::iterator<std::forward_iterator_tag, key_type> key_end);
Run Code Online (Sandbox Code Playgroud)
但是,这限制了我特别使用forward_iterator_tag迭代器,因此尝试调用这样的函数:
remove<char, char>(std::iterator<std::random_access_iterator_tag, char>(), std::iterator<std::random_access_iterator_tag, char());
Run Code Online (Sandbox Code Playgroud)
将失败,因为编译器无法将a转换std::iterator<std::random_access_iterator_tag,...>为a std::iterator<std::forward_access_iterator_tag,...>.此外,此方法不适用于字符串迭代器,矢量迭代器或其他stl迭代器.
有人知道stl如何实现容器/字符串以接受彼此的迭代器吗?例如,这正确编译:
std::string a = "hello";
std::vector<char> v(a.begin(), a.end());
Run Code Online (Sandbox Code Playgroud) 如何为包含空格的路径设置authz文件?
我尝试了各种方法来逃避空间,但没有一个工作.
[/"some path"]
[/some%20path]
[/some\ path]
[\"some path\"]
Run Code Online (Sandbox Code Playgroud) 可能是我能想到的最短的工作示例:
CMakeLists.txt:
project(myprogs)
cmake_minimum_required(VERSION 2.8)
add_executable(myprog2 main.c)
add_executable(myprog main.cpp)
add_library(mylib SHARED mylib.c)
target_link_libraries(myprog2 mylib)
target_link_libraries(myprog mylib)
Run Code Online (Sandbox Code Playgroud)
main.c/main.cpp(相同内容):
#include "mylib.h"
int main(int argc, char** argv)
{
doit();
}
Run Code Online (Sandbox Code Playgroud)
mylib.h:
#ifndef MYLIB_H
#define MYLIB_H
void doit(void);
#endif
Run Code Online (Sandbox Code Playgroud)
mylib.c:
#include "mylib.h"
#include <stdio.h>
void doit(void)
{
printf("doit");
}
Run Code Online (Sandbox Code Playgroud)
系统:
当我这样做时make myprog,myprog链接阶段抱怨有一个未定义的引用doit.但是,如果我使用make myprog2,一切都正确链接,程序按预期运行.
我不明白为什么CMake没有正确mylib地在C++程序中正确链接.从编译器获得详细的输出(我已经修剪了一些到系统库路径/目标文件的链接):
"/ usr/bin/ld"-export-dynamic --eh-frame-hdr -m elf_x86_64 -dyna mic-linker …
我正在尝试使用Java套接字向浏览器发送简单的HTML响应.
这是我的Java代码:
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s;
// this is a test code which just reads in everything the requester sends
while ((s = in.readLine()) != null)
{
System.out.println(s);
if (s.isEmpty())
{
break;
}
}
// send the response to close the tab/window
String response = "<script type=\"text/javascript\">window.close();</script>";
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("Content-Length: " + response.length());
out.println();
out.println(response);
out.flush();
out.close();
socket.close();
Run Code Online (Sandbox Code Playgroud)
server 是一个ServerSocket设置为自动选择要使用的开放端口.
这个想法是任何重定向到http:\\localhost:port(port …
我不知道在理解AVX内在函数如何工作时是否缺少某些东西std::array,但是当我将两者合并时,我对Clang有一个奇怪的问题.
示例代码:
std::array<__m256, 1> gen_data()
{
std::array<__m256, 1> res;
res[0] = _mm256_set1_ps(1);
return res;
}
void main()
{
auto v = gen_data();
float a[8];
_mm256_storeu_ps(a, v[0]);
for(size_t i = 0; i < 8; ++i)
{
std::cout << a[i] << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.5.0的输出(上面4个浮点数是垃圾数据):
1 1 1 1 8.82272e-39 0 5.88148e-39 0
GCC 4.8.2/4.9.1的产出(预期):
1 1 1 1 1 1 1 1
如果我不是传递v到gen_data作为输出参数它适用于两种编译器就好了.我愿意接受这可能是Clang中的一个错误,但是我不知道这可能是未定义的行为(UB).使用Clang 3.7*(svn build)和Clang进行测试现在似乎给出了我的预期结果.如果我切换到SSE 128位内在函数(__m128),那么所有编译器都会给出相同的预期结果.
所以我的问题是: