我正在尝试在编译时交换可变参数模板的两个参数:
template<int...Numbers>struct sequence{};
template<size_t first,size_t second>
struct Swap_Pair
{
const static size_t First = first;
const static size_t Second = second;
};
template <int...Numbers,class swap_pair>
struct Swap_Data
{
static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ?
};
Run Code Online (Sandbox Code Playgroud)
用例应该是:
sequence<1, 2, 3, 4, 5, 6> array;
auto result = Swap_Data < array, Swap_Pair<2, 5> > ::data_;
//result is now std::array which contains 1 2 6 4 5 3
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚什么是正确的写作方式Swap_Data.
我如何进行递归交换以交换可变参数并在编译时转换为std …
我正在尝试在另一个标头中使用对象的类型别名而不包括头文件.
我简化的代码版本是:
// A.h
#include <vector>
using Vector=std::vector<int>;
====================================================
//B.h
using Vector;//forward declaration but not working !(Vector has not beed declared)
int foo(Vector*);
====================================================
//B.cpp
#include "A.h"
void foo(Vector*){}
Run Code Online (Sandbox Code Playgroud)
我不想再写using Vector=std::vector<int>;了,B.h因为它的定义必须与in 的定义相同Vector,A.h并且将来可能会改变,因为我的代码具有循环依赖性,所以我不能包含它.
是否using可以在c ++ 11中进行前瞻性声明?
我有一个可变函数,如:
void test(int){}
template<typename T,typename...Args>
void test(int& sum,T v,Args... args)
{
sum+=v;
test(sum,args...);
}
Run Code Online (Sandbox Code Playgroud)
我想将它别名为:
auto sum = test;//error : can not deduce auto from test
int main()
{
int res=0;
test(res,4,7);
std::cout<<res;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用std::bind但是它不适用于可变参数函数,因为它需要占位符...
是否可以为可变参数函数添加别名?
当你srand()在函数内部调用时,它是否只rand()在该函数内部生成?
下面是函数main在那里srand()被调用.
int main(){
srand(static_cast<unsigned int>(time(0)));
random_number();
}
void random_number(){
rand();
}
Run Code Online (Sandbox Code Playgroud)
该功能random_number在那里rand()使用的是地方外srand()被调用.
所以我的问题是 - 如果你rand()通过使用种子srand(),你可以使用种子rand()外面的种子srand()吗?包括功能,不同文件等
我的代码有问题
这是它的简化版本:
#include <iostream>
class A
{
public :
template <class T>
void func(T&&)//accept rvalue
{
std::cout<<"in rvalue\n";
}
template <class T>
void func(const T&)//accept lvalue
{
std::cout<<"in lvalue\n";
}
};
int main()
{
A a;
double n=3;
a.func(n);
a.func(5);
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
in lvalue
in rvalue
Run Code Online (Sandbox Code Playgroud)
但它是
in rvalue
in rvalue
Run Code Online (Sandbox Code Playgroud)
为什么?!
在这个简化的代码中:
template <int... vars>
struct Compile_Time_Array_Indexes
{
static std::array < int, sizeof...(vars)> indexes;//automatically fill it base on sizeof...(vars)
};
template <int ... vars>
struct Compile_Time_Array :public Compile_Time_Array_Indexes<vars...>
{
};
Run Code Online (Sandbox Code Playgroud)
我想indexes根据vars...尺寸自动填充.
示例:
Compile_Time_Array <1,3,5,2> arr1;//indexes --> [0,1,2,3]
Compile_Time_Array <8,5> arr2; // indexes --> [0,1]
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
例如
template<class T, class U>
void f();
template<class T> using g = f<T, int>;
Run Code Online (Sandbox Code Playgroud)
或任何类似的功能的想法?
我在C++ 11中有一个包装函数,设计用于lambdas,如下所示:
template<typename Func>
int WrapExceptions(Func&& f)
{
try
{
return f();
}
catch(std::exception)
{
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以这样称呼它:
int rc = WrapExceptions([&]{
DoSomething();
return 0;
});
assert(rc == 0);
Run Code Online (Sandbox Code Playgroud)
生活还可以.但是,我想要做的是重载或专门化包装函数,这样当内部函数返回void时,外部函数返回默认值0,例如:
int rc = WrapExceptions([&]{
DoSomething();
});
assert(rc == 0);
Run Code Online (Sandbox Code Playgroud)
我可以在C++ 11中实际执行此操作吗?我不能为我的生活思考如何.
我想使用RichText. 我试过<center>标签和text-align属性。它们都不起作用。
import QtQuick 2.0
import QtQml.Models 2.1
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
ApplicationWindow{
width: Screen.width; height: Screen.height; color: "#d5d6d8"
visible:true
Rectangle {
color: "#33cc94"
width: parent.width; height: parent.height
id : main
Flickable {
id:textArea
anchors.fill: parent
contentHeight: textBrowser.height
contentWidth: textBrowser.width
enabled: false
Text {
id:textBrowser
textFormat: Text.RichText
// visible:false
// text: "<center><img src=\"Icons/TroopCards/ArchersCard.png\" /></center>";
text: "<p style=\"text-align:center\">
<img src=\"Icons/TroopCards/ArchersCard.png\" />
</p>
<p style=\"text-align:center\">Some text at middle</p>";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
如何在 …
我正在尝试编译以下代码:
#include <iostream>
template<template <typename...> class Container,class... Args>
struct Container
{};
template<class T1,class T2>
struct Store
{};
int main()
{
Container<Store,int,double> a;
}
//g++ -Wall -std=c++11 main.cpp
Run Code Online (Sandbox Code Playgroud)
我正在使用gcc 4.8.1,我收到以下错误:
internal compiler error: Segmentation fault
struct Container
^
Run Code Online (Sandbox Code Playgroud)
为什么gcc不编译呢?这段代码是否正确?