小编gia*_*uca的帖子

CLion无法解析外部库中的标头

前段时间我使用XCode在C++ 1x中启动了一个大的头库.库的当前布局是()类似(部分输出ls -R sponf)

sponf/sponf:
ancestors        sponf.h                sponf_utilities.h
categories       sponf_children.h       utilities
children         sponf_macros.h           

sponf/sponf/ancestors:
function.h       meter.h        set.h                    simulation.h

sponf/sponf/categories:
free_space.h     prng.h         random_distribution.h    series.h

sponf/sponf/children:
distributions    histogram.h    random                   simulations
meters           numeric        series                   spaces

sponf/sponf/children/distributions:
arcsine_der.h    exponential.h
box_muller.h     uniform.h

sponf/sponf/children/meters:
accumulator.h    timer.h

#... other subdirs of 'children' ...

sponf/sponf/utilities:
common_math.h    limits.h       string_const.h

#... other directories ...
Run Code Online (Sandbox Code Playgroud)

我想将这个项目移植到CLion,这似乎是一个非常好的IDE(基于类似的AndroidStudio IDE),但我遇到了一些麻烦.

小测试程序

我试过这个小程序作为测试:

#include <iostream>
#include <sponf/sponf.h>

using namespace std;

int main() {
    using space = sponf::spaces::euclidean_free_space<double, 3>;
    sponf::simulations::random_walk<space> rw;

    rw.step(1);

    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ header-files header-only clion

11
推荐指数
1
解决办法
2万
查看次数

__typeof__宏扩展为函数名

我在plain C中编写了以下代码:

#define _cat(A, B) A ## _ ## B
#define cat(A, B) _cat(A, B)
#define plus(A, B) cat(cat(plus,__typeof__(A)),__typeof__(B))(A, B)

int main(int argc, const char * argv[])
{
    double x = 1, y = 0.5;
    double r = plus(x, y);
    printf("%lf",r);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我希望将宏plus扩展为包含参数类型的函数名.在这个例子中,我希望它扩展以下方式

double r = plus(x, y)
...
/* First becomes*/
double r = cat(cat(plus,double),double)(x, y)
...
/* Then */
double r = cat(plus_double,double)(x, y)
...
/* And finally */
double r = plus_double_double(x, …
Run Code Online (Sandbox Code Playgroud)

c macros posix typeof c-preprocessor

5
推荐指数
1
解决办法
1429
查看次数

许多功能的相同模板

大家好(祝新年快乐!)

我正在用C++写一个(不是真的)简单的项目(我的第一个,来自普通的C).我想知道是否有办法简化具有相同模板模式的多个函数的定义.我想一个例子可以更好地解释这个问题.

上下文

假设我有一个"Set"类,它代表一个数字列表,定义为

template <class T>
class Set {
    static_assert(std::is_arithmetic<T>(), "Template argument must be an arithmetic type.");
    T *_address;
    ...
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我有一个实例(比如说Set<double>)和一个数组U array[N],其中U是另一个算术类型并且N是一个整数,我希望能够执行一些操作,例如将数组的值赋给那些数组Set.因此我在类中创建了函数模板

template <class U, int N>
void assign(U (&s)[N]) {
    static_assert(std::is_arithmetic<U>(), "Template argument must be an arithmetic type.");
    errlog(E_BAD_ARRAY_SIZE, N == _size);
    idx_t i = 0;
    do {
        _address[i] = value[i];
    } while (++i < size);
}
Run Code Online (Sandbox Code Playgroud)

问题

至于我的测试,上面的代码完全正常.但是我发现真的很难看,因为我需要static_assert确保只将算术类型作为参数(参数U),我需要一种方法来确保数组大小(参数N).另外,我不与完成assign的功能,但我需要这么多的其他功能,如 …

c++ templates preprocessor-directive

0
推荐指数
1
解决办法
330
查看次数