编辑:
如果你落在这篇文章上,你可能想直接跳到答案
今天早上我发了一篇关于我的困惑的帖子
机器类型(C++ librairies):i386 vs x86_64
但我想我做错了不准确.所以我决定举一个我面对的情况的例子,我无法理解.
步骤1
我在机器A上构建了一个库,一个2岁的mac,OS x 10.7.5(我猜是64位;我的猜测基于你将在下面的附加信息中看到的命令)使用以下文件.
标题SimpleClass.hpp:
#ifndef SIMPLECLASS_HPP
#define SIMPLECLASS_HPP
class SimpleClass
{
public:
SimpleClass();
SimpleClass(const SimpleClass& orig);
virtual ~SimpleClass();
private:
} ;
#endif /* SIMPLECLASS_HPP */
Run Code Online (Sandbox Code Playgroud)
源文件SimpleClass.cpp:
#include "SimpleClass.h"
#include <iostream>
SimpleClass::SimpleClass()
{
std::cout << "A new instance of Simple Class was created" << std::endl;
}
SimpleClass::SimpleClass(const SimpleClass& orig)
{
}
SimpleClass::~SimpleClass()
{
}
Run Code Online (Sandbox Code Playgroud)
我使用创建库
~/cpp_test$ clang++ -c -o SC.o -I SimpleClass.hpp SimpleClass.cpp
~/cpp_test$ ar rcs libtest_sc.a SC.o
Run Code Online (Sandbox Code Playgroud)
机器A的其他信息:
~/cpp_test$ clang++ --version …Run Code Online (Sandbox Code Playgroud) 我们有一个包含N个文件夹的git存储库.
Repo
|-Folder1
|-Folder2
|- ...
|-FolderN
Run Code Online (Sandbox Code Playgroud)
我们希望与不同的合作者共享不同的文件夹.每个协作者只能访问其允许的文件夹子集.使用git实现这一目标的"好"方法是什么?
答案是使用git submodules.但在我读完这篇文章之后:https:
//codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/
我明白你需要掌握好git(这不是我们的合作者的情况),以便在使用时没有问题git submodules.
我读到了一些可能的替代品,比如gitslave,和git subtree.gitslave似乎是一个很好的解决方案,但在我看来仍然是一个复杂的解决方案
这是我的简单解决方案,我想知道它是否有一些非常糟糕的缺点:
- 为每个文件夹和Repo存储库提供一个简单的存储库.然后在主Repo中添加Folder1,...,FolderN中的所有文件.
-globalpush脚本:
function globalpush(){
REPOS="$HOME/Repo/
$HOME/Repo/Folder1
$HOME/Repo/Folder2
$HOME/Repo/Folder3
# ...
$HOME/Repo/FolderN"
#do not show untracked files
git config status.showuntrackedfiles no
read -p "Commit description: " description
for repo in ${REPOS}
do
# if the repo folder exists
if [ -d $repo ]
then
# Go inside the repo
cd $repo
echo …Run Code Online (Sandbox Code Playgroud) 在julia中,我们如何知道某个类型是通过值还是通过引用来操纵的?
以java为例(至少对于sdk):
基本类型(名称以小写字母开头的类型,如"int")由值操纵
对象(名称以大写字母开头的对象,如"HashMap")和数组通过引用进行操作
因此很容易知道在函数内修改的类型会发生什么.
我很确定我的问题是重复但我找不到复制...
编辑
这段代码:
function modifyArray(a::Array{ASCIIString,1})
push!(a, "chocolate")
end
function modifyInt(i::Int)
i += 7
end
myarray = ["alice", "bob"]
modifyArray(myarray)
@show myarray
myint = 1
modifyInt(myint)
@show myint
Run Code Online (Sandbox Code Playgroud)
回报:
myarray = ASCIIString["alice","bob", "chocolate"]
myint = 1
Run Code Online (Sandbox Code Playgroud)
这对我来说有点混乱,我之所以提交这个问题.@StefanKarpinski的评论澄清了这个问题.
我的困惑来自于我认为+ =作为操作员,像推的方法!这是修改对象本身.但事实并非如此.
i += 7应该被视为i = i + 7(与不同对象的绑定).实际上,modifyArray如果我使用例如,这种行为将是相同的a = ["chocolate"].
这是我所说的代码部分.
try {
std::cerr << "first try" << std::endl;
po::store(po::parse_config_file(ifs, _configFileOptions, false), vm);
} catch(...) {
std::cerr << "second try" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
只是为了寻找细节,我使用boost program_options来解析配置文件.由于我在文件中添加了一个无法识别的选项,因此引发了异常.
Clang没有捕获此异常存在问题.基本上我只在输出中看到
first try
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> >: unrecognised option 'TestFrequency'
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)
这是我的铿锵版:
c++ --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)
编辑:当没有异常时,解析和一切正常.
考虑具有属性的基类
class Base
{
protected:
AttributeBase * elementPtr;
...
};
Run Code Online (Sandbox Code Playgroud)
和派生类
class Derived : public Base
{
...
};
Run Code Online (Sandbox Code Playgroud)
我也有一个AttributeDerived来自的课程AttributeBase
当我创建类的对象时,Base我希望elementPtr以这种方式初始化:
elementPtr = new AttributeBase()
Run Code Online (Sandbox Code Playgroud)
但是当我创建类的对象时,Derived我希望elementPtr以这种方式初始化:
elementPtr = new AttributeDerived()
Run Code Online (Sandbox Code Playgroud)
最干净的方法是什么?
我已经下载了一个在GCC中ABI更改之前使用gcc 4.8编译的库.
在我的笔记本电脑上(最新的kubuntu)我有GCC 5.2.当我安装boost时,似乎它使用了新的ABI,但后来我得到了以下链接错误
未定义的符号.....__ cxx11 ....
如何使用旧的ABI和GCC5安装boost?
我无法解释插入新元素时std :: set所做的比较次数.这是一个例子:
对于此代码
struct A {
int i = 0;
bool operator()(int a, int b)
{
++i;
return a < b;
}
};
int main()
{
A a;
set<int, A> s1(a);
s1.insert(1);
cout << s1.key_comp().i << endl;
s1.insert(2);
cout << s1.key_comp().i << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是
0
3
Run Code Online (Sandbox Code Playgroud)
为什么插入第二个元素需要进行3次比较?O_O
在这样的代码中:
Comparator comp(3);
set<string, Comparator> s1(comp);
set<string, Comparator> s2(comp);
set<string, Comparator> s3(comp);
set<string, Comparator> s4(comp);
Run Code Online (Sandbox Code Playgroud)
比较器的实际实例(即comp)在每次创建set对象时被复制为cpp引用状态
容器保留alloc和comp的内部副本,用于分配存储并在整个生命周期内对元素进行排序.
所以我们想知道这在C++中是否合法
#include <set>
#include <iostream>
struct A {
int i = 0;
bool operator()(int a, int b)
{
++i;
return a < b;
}
};
int main()
{
A a;
std::set<int, A&> s1( {1, 2, 3}, a);
std::set<int, A&> s2( {4, 5, 6}, a);
std::cout << a.i;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我有一个struct/class,它有许多属性,其中一个是principal.在构造此类的实例时,必须在参数中给出principal属性,但是可以提供或不提供任何其他属性.如果未将属性作为参数提供,则应从principal属性计算该属性.
我不知道如何在不编写指数的构造函数的情况下编写这样的类; 或者在每个构造函数之后使用setter,它只有一个参数对应于principal属性.
我在这里给出一个最适合我的例子.但这当然不能编译:
#include <cstdlib>
#include <iostream>
using namespace std;
struct StringInfo
{
string str;
int importance;
StringInfo(string strP, int importanceP = strP.length()):
str(strP), importance(importanceP){}
};
int main(int argc, char** argv) {
string test = "test";
StringInfo info(test);
cout << info.importance << endl;
}
Run Code Online (Sandbox Code Playgroud)
你有更好的(非指数)解决方案吗?提前致谢.
我对不同的机器架构(32位,64位,......)了解甚少.正因为如此,我经常在不同的机器上使用C++库时遇到困难,因为它们被困在"令人讨厌的"未定义的架构符号......".
如果有人可以向我解释为什么当我在同一台机器上使用以下命令(一个2年前使用山狮操作系统的mac)时,我会很高兴为什么会得到这样令人困惑的答案.该man uname指示
-m打印机器硬件名称.
-p打印机器处理器体系结构名称.
uname -p,我得到:i386(这意味着32位如果我没有错).
lipo -info lib_test.a返回:输入文件lib_test.a不是胖文件
非胖文件:lib_test.a是架构:x86_64(这意味着64位如果我没有错)
uname -m是x86_64的
c++ ×7
32bit-64bit ×2
constructor ×2
linker ×2
macos ×2
set ×2
stl ×2
abi ×1
boost ×1
c++11 ×1
clang ×1
comparator ×1
functor ×1
gcc ×1
git ×1
git-slave ×1
git-subrepo ×1
git-subtree ×1
inheritance ×1
julia ×1
linux ×1
parameters ×1
templates ×1