有没有一种方法可以确定是否可以对constexpr求值,并将结果用作constexpr布尔值?我的简化用例如下:
template <typename base>
class derived
{
template<size_t size>
void do_stuff() { (...) }
void do_stuff(size_t size) { (...) }
public:
void execute()
{
if constexpr(is_constexpr(base::get_data())
{
do_stuff<base::get_data()>();
}
else
{
do_stuff(base::get_data());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是C ++ 2a。
我发现了以下reddit线程,但我不是宏的忠实拥护者。https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/
我想constexpr if在编译时使用分支,但最新的MSVC编译器似乎不支持它.有以下替代方案吗?:
template<typename T>
void MyFunc()
{
if constexpr(MeetsConditions<T>::value)
{
FunctionA<T>();
}
else
{
FunctionB<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之:我可以模拟constexpr if编译器不支持的时间吗?
我可以使用 CMake 成功构建我的项目,但是我可以使用它来安装结果吗?
随着Make我添加目标install并从命令行调用它。我无法弄清楚 CMake 是否可以实现。
最终目标是以平台可移植的方式安装静态库、动态库和相应的头文件。我怎么想它会的工作:在Linux上,复制/usr/include及/usr/lib。在 Windows 上,它可能是用户提供的文件夹,其中包含一个 include 和 lib 文件夹。
在安装规则表明,这样的事情是可能的。但我如何实际使用它?
目前我执行以下操作:
在这里,我希望做这样的事情:
采取方法self和采取方法&self甚至是方法有什么区别&mut self?
例如
impl SomeStruct {
fn example1(self) { }
fn example2(&self) { }
fn example3(&mut self) { }
}
Run Code Online (Sandbox Code Playgroud)
假设我想实现一种将结构体漂亮地打印到标准输出的方法,我应该采用&self吗?我猜self也有效?我不确定何时使用什么。
是否可以有一个专门的模板类,它是模板类的成员(本身不是专门的)?
对于非模板父类,它可以工作:
class owner
{
template<int num>
struct s
{
int ret() { return num; }
};
};
template<>
struct owner::s<0>
{
int ret() { return 0; }
};
Run Code Online (Sandbox Code Playgroud)
但是在制作owner模板类时,它不会:
template<typename some>
class owner
{
template<int num>
struct s
{
int ret() { return num; }
};
};
template<typename some>
struct owner<some>::s<0>
{
int ret() { return 0; }
};
Run Code Online (Sandbox Code Playgroud)
搜索表明函数不可能(?),但是类/结构怎么样? 专门化模板类的模板成员
我正在使用类的速记版本,如下所示:
using NodeSteps = Tuple<Node, int>;
Run Code Online (Sandbox Code Playgroud)
Node是我自己定义的类.这通常很好,但问题是,Node是一个需要结构的泛型.
我的问题如下:
1.如何在C#中调用这些typedef.我知道它们并不完全是typedef,但它是我能想到的最相似的东西.
2.我如何制作通用版本?
using NodeSteps<T> = Tuple<Node<T>, int>;
Run Code Online (Sandbox Code Playgroud)
我注意到这不是这样做的方法.我也想指定T是一个结构.
我正在使用哈希集,其中我存储整数数组(32位).这意味着我需要一个算法来散列整数数组.我正在寻找一个32位整数(C#int)哈希.
我已经尝试并编辑了两个现有的算法,你可以看到底部的四个版本,包括它们的基准.
我的问题如下:
1.您认为底层算法是否适用于此目的?
2.有没有更好的算法可用于此目的?
计划信息
16 entries整数smaller than 10,但两者都必须支持更大的值.我可以说有机会发生的最大值是200个条目和值为20的整数.基准和代码
下面是我的基准测试和代码,从我的程序中的最差到最佳性能.
356525const uint seed = 144MurMurHash3使用从坐标直接检索的字节
代码等于https://gist.github.com/automatonic/3725443 使用以下代码检索字节数组:
int size = Marshal.SizeOf(typeof(Coordinates2D));
int length = carCoords.Length;
Byte[] bytes = new Byte[size * length];
for (int i = 0; i < length; ++i)
{
GCHandle pinStructure = GCHandle.Alloc(carCoords[i], GCHandleType.Pinned);
Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, i*size, size);
pinStructure.Free();
}
// Hash the byte array
return MurMurHash3.Hash(new System.IO.MemoryStream(bytes)); …Run Code Online (Sandbox Code Playgroud) 假设我有一个可变参数模板类.如何创建一个函数,使其参数属于集合类型,例如int,参数的数量等于模板类型的数量?
template <typename... Types>
class Test
{
public:
void Func(???); // I don't know how to declare such a function
}
Test<string, bool, long> myTest; // Three types
myTest.Func(905, 36, 123315); // Three arguments, but always of type int.
Run Code Online (Sandbox Code Playgroud)
最后,该函数的目标是返回所提供的int的元组.为简单起见,我在示例代码中将函数显示为void.
假设我有一个可变数量的参数,我想将它们相乘.我想到的第一种方法是递归算法:
template<typename Head>
u64 Multiply(Head head) const
{
return head;
}
template<typename Head, typename... Tail>
u64 Multiply(Head head, Tail... tail) const
{
return head * Multiply(tail...);
}
Run Code Online (Sandbox Code Playgroud)
但后来我看到了这个伎俩:
// u32 and u64 are 32 and 64 bit unsigned integers.
template<typename... T>
u64 Multiply(T... args)
{
u64 res = 1;
for (const u32& arg: {args...})
res *= arg;
return res;
}
Run Code Online (Sandbox Code Playgroud)
第二个看起来对我来说更好.更容易阅读.但是,这如何影响性能呢?有什么东西被复制了?{args...} 首先做什么?有更好的方法吗?
我可以访问C++ 14.
编辑要清楚:它是关于运行时乘法,而不是编译时间.
更清楚:我不想有必要计算整数(虽然那是我当前的应用程序),但我发现的算法专门用于整数.
更多:参数属于同一类型.没有这种限制的算法会非常有趣,但也许是针对不同的问题.
最近发布了 boost 1.64,包括 boost::process。这为启动进程提供了一个简单的界面。以前我使用的是 boost::process 库的独立版本(请参阅此处)。这工作得很好。我想更改为新版本,以便可以删除独立依赖项。
API 有点不同,但一切正常,除了 on thing。在旧版本中,我能够传递特定于 Windows 的上下文对象,该对象允许我隐藏进程打开的任何控制台窗口。
boost::process::win32_context ctx;
ctx.environment = boost::process::self::get_environment();
STARTUPINFOA stup;
ZeroMemory(&stup, sizeof(stup));
stup.cb = sizeof(stup);
stup.dwFlags = STARTF_USESHOWWINDOW;
stup.wShowWindow = SW_HIDE;
ctx.startupinfo = &stup;
std::vector<std::string> args;
boost::process:child process = boost::process::win32_launch("myprogram", args, ctx);
Run Code Online (Sandbox Code Playgroud)
使用新版本它看起来像这样:
boost::process::environment env = boost::this_process::environment();
boost::process:child process(boost::filesystem::path("myprogram"), env);
Run Code Online (Sandbox Code Playgroud)
除了隐藏控制台窗口外,一切正常。有可能实现这一目标吗?