小编Ale*_* B.的帖子

C++赋值副作用

在尝试确保两个变量的升序时,我在Visual Studio 2012 C++编译器中遇到了奇怪的异常,可以通过以下代码片段来说明

    double x1 = 2;
    double x2 = 1;
    std::tie(x1, x2) = std::minmax(x1, x2);
    std::cout << "x1 = " << x1 << ",   x2 = " << x2 << "\n";
Run Code Online (Sandbox Code Playgroud)

人们会期望x1是1而x2是2.但它们不是.代替

    //output:
    //x1 = 1,   x2 = 1
Run Code Online (Sandbox Code Playgroud)

有没有什么好的解释,只是为了确保不再陷入类似的陷阱?

c++ tuples visual-studio-2012

6
推荐指数
1
解决办法
153
查看次数

Invert tuple arguments

I want to write a template class InvTuple which defines type as a tuple of class arguments in inverse order. So it should work like

InvTuple<T1, T2, T3, ...>::type   --->   tuple<..., T3, T2, T1>
Run Code Online (Sandbox Code Playgroud)

I defined it like so

template<class...T>
struct InvTuple;

template<class T1, class...T>
struct InvTuple < T1, T... >
{
    template<class... U>
    using doInvert = typename InvTuple<T...>::doInvert < U..., T1 > ;  
                     // <--- unrecognizable template declaration/definition, 
                     // syntax error : '<'

    using type = doInvert<>;
};

template<> …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates

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

从std :: tuple成员中删除引用

我在stl元组的帮助下为一些变量实现了保存/恢复功能,如下所示:

double a = 1, b = 2;
int c = 3;
auto tupleRef = std::make_tuple(std::ref(a), std::ref(b), std::ref(c));

// here I'm saving current state of a, b, c
std::tuple<double, double, int> saved = tupleRef;

//here goes block of code, where a, b, and c get spoiled
......................
//

//now I'm restoring initial state of a, b, c
tupleRef = savedTuple;
Run Code Online (Sandbox Code Playgroud)

这段代码效果很好.但不是明确指定元组成员类型

std::tuple<double, double, int> saved = tupleRef;
Run Code Online (Sandbox Code Playgroud)

我想宁愿删除所有tupleRef成员的引用,如下所示

auto saved = remove_ref_from_tuple_members(tupleRef);
Run Code Online (Sandbox Code Playgroud)

我相信可以为此编写"remove_ref_from_tuple_members"模板.

谢谢你的回答.

c++ tuples std visual-studio-2010

4
推荐指数
1
解决办法
1753
查看次数

庞大的宏定义超载

我想定义宏像

#define DECLARE_FUNCTION(funcName, retType, args) retType funcName(args)
Run Code Online (Sandbox Code Playgroud)

并使用它

DECLARE_FUNCTION(intFunc, int, void);
DECLARE_FUNCTION(voidFunc, void, double, double);
DECLARE_FUNCTION(doubleFunc, double, int, double, double);
Run Code Online (Sandbox Code Playgroud)

期待那些将扩展到

int intFunc(void);
void voidFunc(double, double);
double doubleFunc(int, double, double);
Run Code Online (Sandbox Code Playgroud)

这当然不起作用,因为用三个参数定义的宏会占用所有"冗余"参数,结果是

int intFunc(void);
void voidFunc(double);
double doubleFunc(int);
Run Code Online (Sandbox Code Playgroud)

我不介意定义不同的情况下宏,像 DECLARE_FUNCTION_WITH_0_ARGS,DECLARE_FUNCTION_WITH_1_ARG,DECLARE_FUNCTION_WITH_2_ARGS等等.但问题是,这些宏并不像我在本例中得到以原始的,它们包含了很多行代码,这将是很好的不重写它们,但只定义一个非平凡的宏,eq DECLARE_FUNCTION_WITH_1_ARG,并从所​​有其他宏的实体中调用它.

c c++ macros

3
推荐指数
1
解决办法
92
查看次数

可变结构规范

我定义了一个可变参数结构

template <class T, class... TRest>
struct Opa
{
    Opa()
    {
        std::cout << "Mutiple-arguments template";
    }
};
Run Code Online (Sandbox Code Playgroud)

并希望专门针对具有1个参数的情况进行专门化,如下所示

template <>
struct Opa<class T>
{
    Opa()
    {
        std::cout << "One-argument template";
    }
};
Run Code Online (Sandbox Code Playgroud)

但编译器只是忽略了第二个结构,而输出来自

Opa<int> opa;
Opa<int, int> opa_opa;
Run Code Online (Sandbox Code Playgroud)

Mutiple-arguments template, Mutiple-arguments template.

以不同方式指定单参数模板,例如

template <class T>
struct Opa
{...}
Run Code Online (Sandbox Code Playgroud)

导致编译错误.我意识到我的问题很简单,但谷歌搜索没有帮助.所以请不要把腐烂的西红柿扔给我,谢谢你的回答.

c++ variadic-templates

3
推荐指数
1
解决办法
143
查看次数

模板内的递归类型定义

我想定义与可变参数模板的帮助下多维数组vector<vector<....>>,但偶然不可能定义类型initializer_list<initializer_list<...>>,这是需要初始化列表构造函数.这可以通过以下代码进行简要说明

template<class T, size_t dim>
class MyArr : vector < MyArr < T, dim - 1 >>
{
public:
    typedef initializer_list<MyArr < T, dim - 1 >::ListType> ListType;
    //using ListType = typename initializer_list<MyArr < T, dim - 1 >::ListType>;
};

template<class T>
class MyArr<T, 1> : vector < T>
{
public:
    typedef initializer_list<T> ListType;
};
Run Code Online (Sandbox Code Playgroud)

编译器显示"错误C2923:'的std :: initializer_list’:'myArr,该:: ListType’不是参数'_Elem’有效的模板类型参数".

定义ListType类型的正确方法是什么?谢谢你的回答.

c++ variadic-templates

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