我正在学习Go,我对何时使用指针感到困惑.具体来说,当struct从函数返回a时,何时返回struct实例本身是合适的,何时返回指向struct的指针是否合适?
示例代码:
type Car struct {
make string
model string
}
func Whatever() {
var car Car
car := Car{"honda", "civic"}
// ...
return car
}
Run Code Online (Sandbox Code Playgroud)
我想要返回指针的情况是什么,我不希望在哪里?有一个很好的经验法则吗?
>> C = [{1} {2} ; {'@CF'} {2}]
C =
[ 1] [2]
'@CF' [2]
>> whos C
Name Size Bytes Class Attributes
C 2x2 478 cell
Run Code Online (Sandbox Code Playgroud)
我怎样才能转换C成double:
>> C
C =
1 2
NaN 2
Run Code Online (Sandbox Code Playgroud)
我试过了str2double(C).它返回:
NaN NaN
NaN NaN
Run Code Online (Sandbox Code Playgroud) 我在C++ 11中看到了以下enable_if示例:
struct is_64_bit
{
static const bool value = sizeof(void*) == 8;
};
enable_if<is_64_bit::value, void>::type
my_memcpy(void* target, const void* source, size_t n)
{
cout << "64 bit memcpy" << endl;
}
enable_if<!is_64_bit::value, void>::type
my_memcpy(void* target, const void* source, size_t n)
{
cout << "32 bit memcpy" << endl;
}
Run Code Online (Sandbox Code Playgroud)
据我了解,根据系统架构,"my_memcpy"函数可用于32位或64位版本.但是我在编译时遇到以下错误:
error: ‘type’ in ‘struct std::enable_if<false, void>’ does not name a type
Run Code Online (Sandbox Code Playgroud)
我有点困惑,因为我认为只有32版本可用(我使用的是Linux Fedora 32位).
这个例子可能有问题吗?还是我错过了什么?
谢谢.
根据cppreference,std :: basic_ostringstream可以使用自定义分配器进行实例化.从C++ 11开始,允许分配器具有状态,并且我的自定义分配器类确实具有内部每实例状态(实际上每个分配器实例都有一个指向MemPool类实例的指针,它转发所有分配请求).这就是为什么我的分配器类型不能简单地默认构造并且期望工作,因为毕竟,它将从哪里获取它应该使用的MemPool实例的地址?
不幸的是,如果你看看在构造函数原型的标准:: basic_ostringstream,你可以看到,没有任何支援的建设者的实际接受一个allocator实例!我很确定std :: basic_ostringstream必须进行一些分配来构造它的结果字符串,我真的希望它使用我的分配器来做那些,但如果我没办法怎么办呢?告诉它使用哪个特定的MemPool实例?
我在这里忽略了什么吗?我觉得我必须拥有,但我肯定不会看到此刻的情况.
我正在尝试operator T()使用SFINAE 重载以在T基本类型时返回副本,并在T类时使用const引用.
在double下面的示例中使用a 时,我无法std::is_class删除第二个重载(with ).
也就是说,我得到的错误是:
error: no type named ‘type’ in ‘struct std::enable_if<false, const double&>’
operator typename std::enable_if< std::is_class<T>::value, const T&>::type () const
^
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
#include <iostream>
#include <type_traits>
template<typename T>
struct Foo
{
operator typename std::enable_if<!std::is_class<T>::value, T >::type () const
{
return _val;
}
operator typename std::enable_if< std::is_class<T>::value, const T&>::type () const
{
return _val;
}
T _val;
};
int main()
{
Foo<double> f1;
f1._val = 0.3; …Run Code Online (Sandbox Code Playgroud) 假设我们有以下代码.我们有以下课程
_
class Animal
{
public:
Animal();
void HasWings() = 0;
};
class Bird : public Animal
{
public:
Bird() : Animal() {}
void HasWings() override { return true; }
};
class Dog : public Animal
{
public:
Dog() : Animal() {}
void HasWings() override { return false; }
};
class Zoo
{
public:
Zoo() {}
void AddAnimal(Animal* animal) { _animals.push_back(animal); }
...
std::vector<Animal*> _animals;
};
void myTest()
{
Zoo myZoo;
Bird* bird = new Bird();
Dog* …Run Code Online (Sandbox Code Playgroud) 这是一个10行的C++ 11程序,从我正在编写的程序中大大简化:
template <typename T> class Base { public:
template <typename S> Base(S x) {}
};
template <typename T> class Child : public Base<T> { public:
using Base<T>::Base;
};
template <> class Child<int> : public Base<int> { public:
using Base<int>::Base;
};
int main()
{
Child<int> child(8.0f);
}
Run Code Online (Sandbox Code Playgroud)
MSVC 2015产出:
1>------ Build started: Project: MyProject, Configuration: Debug Win32 ------
1> filename.cpp
1>path\to\filename(10): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1393)
1> To …Run Code Online (Sandbox Code Playgroud) c++ visual-c++ c++11 internal-compiler-error visual-studio-2015
我正在尝试使用C++字符串类的统一初始化程序.以下是代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 {"aaaaa"};
string str2 {5, 'a'};
string str3 (5, 'a');
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
cout << "str3: " << str3 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
str1: aaaaa
str2: a
str3: aaaaa
Run Code Online (Sandbox Code Playgroud)
这让我摸不着头脑.为什么str2不能达到预期的效果str3呢?
我有一个struct包含C风格的数组数据成员.我想将这个结构体暴露给Python,并且这个数据成员可以作为Python中的列表访问.
struct S
{
char arr[4128];
};
void foo( S const * )
{}
BOOST_PYTHON_MODULE( test )
{
using namespace boost::python;
class_<S>( "S" )
.def_readwrite( "arr", &S::arr )
;
def( "foo", foo );
}
Run Code Online (Sandbox Code Playgroud)
上面的代码无法构建
error C2440: '=' : cannot convert from 'const char [4128]' to 'char [4128]'
Run Code Online (Sandbox Code Playgroud)
C风格的数组不可分配,因此错误很有意义.如果我将数据成员更改为普通char而不是数组,则代码将编译.
我无法用一个std::array或其他容器替换该数组,因为该结构正由C API使用.我能想到的唯一解决方案是编写几个包装器并执行以下操作
struct S1重复的,S除了它将有一个std::array而不是一个C风格的数组foo_wrapper接受a的A ,S1 const *将内容复制到S实例并调用footo_python_converter转换std::array为Python列表 …我正在学习C++ 11的rvalue功能.C++ Primer第5版说rvalue引用只能绑定到rvalue,但是当我尝试编译这个程序时,它传递了,并且输出是1 1.
我不明白为什么.我正在使用g ++ 4.4.6,并用它编译
g ++ -Wall -std = c ++ 0x test.cpp -o test
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int &&rr = i;
rr = 1;
std::cout << rr << std::endl;
std::cout << i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×6
boost ×1
boost-python ×1
cell-array ×1
double ×1
enable-if ×1
go ×1
matlab ×1
pointers ×1
python ×1
rvalue ×1
sfinae ×1
string ×1
templates ×1
type-traits ×1
visual-c++ ×1