我有一个非常简单的功能使用static_assert.麻烦的是我想要static_assert在函数声明中涉及的行为 - 特别是推断返回类型.似乎没有任何地方可以插入,static_assert以便我可以在编译器无法推断出返回类型之前触发它.
到目前为止,我将返回类型推导和静态断言放在结构中.这将触发断言,这很好,但它仍然会在类型推导上产生错误,这是我想要消除的噪音.
#include <type_traits>
#include <functional>
#include <memory>
#include <map>
#include <iostream>
#include <string>
#include <cstdio>
#include <tuple>
#include <sstream>
#include <vector>
#include <algorithm>
template<typename T, typename X> struct is_addable {
template<typename Test, typename Test2> static char test(decltype(*static_cast<Test*>(nullptr) + *static_cast<Test2*>(nullptr))*);
template<typename Test, typename Test2> static int test(...);
static const bool value = std::is_same<char, decltype(test<T, X>(nullptr))>::value;
};
template<typename T, typename X> struct is_addable_fail {
static const bool value = is_addable<T, X>::value; …Run Code Online (Sandbox Code Playgroud) templates static-assert generic-programming visual-studio-2010 c++11
假设,我想开发一个通用库,它应该可以用于类似数字的类型,包括双重和用户定义的类型.我现在面临的问题是,我不知道如何编写函数模板的返回类型,就像这样:
template<class T>
auto transmogrify(T x)
-> ???
{
using std::abs;
return abs(x)+2.0;
}
Run Code Online (Sandbox Code Playgroud)
using声明使这个函数模板的主体适用于原始类型,因为它们没有关联的命名空间(因此没有ADL).但是我希望transmogrify使用专门的abs函数,以防用户定义类型的作者提供自己的abs函数.我不能简单地使用
-> decltype( abs(x)+2.0 )
Run Code Online (Sandbox Code Playgroud)
因为这不适用于比赛,因为std :: abs不在范围内(据我所知).但写作
-> decltype( std::abs(x)+2.0 )
Run Code Online (Sandbox Code Playgroud)
会禁用ADL.但禁用ADL不是一种选择.此外,专用abs函数返回的值可能不是T类型,而是某些其他类型.
关于如何解决返回类型问题的任何想法,同时(a)保持ADL和(b)回退到某些默认函数(如本例中的std :: abs),对于不提供专用abs的类型.
c++ generic-programming decltype argument-dependent-lookup c++11
我知道Go不支持模板或重载函数,但我想知道是否有任何方法可以进行某种泛型编程?
我有很多这样的功能:
func (this Document) GetString(name string, default...string) string {
v, ok := this.GetValueFromDb(name)
if !ok {
if len(default) >= 1 {
return default[0]
} else {
return ""
}
}
return v.asString
}
func (this Document) GetInt(name string, default...int) int {
v, ok := this.GetValueFromDb(name)
if !ok {
if len(default) >= 1 {
return default[0]
} else {
return 0
}
}
return v.asInt
}
// etc. for many different types
Run Code Online (Sandbox Code Playgroud)
没有这么多冗余代码,有没有办法做到这一点?
到底发生了什么:
"Couldn't match kind `*' against `#'"
Run Code Online (Sandbox Code Playgroud)
我在使用TemplateHaskell(ghci -XTemplateHaskell)的GHCi中尝试以下内容
$(reify ''Show >>= dataToExpQ (const Nothing))
Run Code Online (Sandbox Code Playgroud)
我希望得到一个Exp(这有一个Show的实例).我这样做是为了在应用程序中插入有关haskell类型的信息,使其可用作实际数据,而不是字符串.
我的目标是:
info :: Info
info = $(reify ''Show >>= dataToExpQ (const Nothing))
Run Code Online (Sandbox Code Playgroud)
我真的不明白这个错误信息,反正是什么'#'?如果有#,是否有也# -> #还是* -> #?它是否与类型相关的类型有关(虽然我不知道那可能是什么)?
好的,所以我现在明白GHC有各种各样的层次结构,而'#'是一种特殊的未装箱类型.一切都很好,但为什么会弹出这个错误?也许未装箱的类型与genercis不相称?
我还不完全确定这对我有意义,因为我认为未装箱的类型是由编译器执行的优化.我还认为,如果存在数据实例,则需要存在可能包含在数据结构中的所有类型.
经过进一步调查,我认为Names存在问题,有没有办法在dataToExpQ中规避它们?怎么用这个论点呢?
以下是g ++(STL的sgi版本)的STL实现的摘录.我想知道为什么他们使用部分特化而不是函数重载.
template <class InputIterator, class OutputIterator>
struct __copy_dispatch
{
OutputIterator operator()(InputIterator first, InputIterator last,
OutputIterator result) {
return __copy(first, last, result, iterator_category(first));
}
};
//If the inputiterator and the outputiterator is all type T
//This is a partial specialization of the generalized version
template <class T>
struct __copy_dispatch<T*, T*>//-----------------------(1)
{
T* operator()(T* first, T* last, T* result) {
typedef typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, result, t());
}
};
//Strictly speaking this is a partial specialization of the last …Run Code Online (Sandbox Code Playgroud) 有什么办法可以得到这样的行为?
// Some definition(s) of operator "" _my_str
// Some definition of function or macro MY_STR_LEN
using T1 = MY_STR_LEN("ape"_my_str);
// T1 is std::integral_constant<std::size_t, 3U>.
using T2 = MY_STR_LEN("aardvark"_my_str);
// T2 is std::integral_constant<std::size_t, 8U>.
Run Code Online (Sandbox Code Playgroud)
似乎不是,因为字符串文字会立即传递some_return_type operator "" _my_str(const char*, std::size_t);给文字运算符模板,而不会传递给文字运算符模板(2.14.8 / 5)。即使几乎总是一个常量表达式,该size函数参数也不能用作模板参数。
但是似乎应该有某种方法可以做到这一点。
标准库确实区分了通用算法的谓词和非谓词版本.例如,std::sort()看起来像:
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
Run Code Online (Sandbox Code Playgroud)
写下面的内容有什么问题吗?
template< class RandomIt, class Compare = std::less<void>>
void sort( RandomIt first, RandomIt last, Compare comp = Compare{});
Run Code Online (Sandbox Code Playgroud) c++ generic-programming c++-standard-library language-lawyer c++14
是否可以在Scala中自动派生密封特征族的订单?
例如,能够这样做会很高兴:
sealed trait Letters
case object A extends Letters
case object B extends Letters
(A < B) == True
Run Code Online (Sandbox Code Playgroud)
这感觉就像Shapeless可以处理的东西,但我不知道这是否存在.
我对这些事情缺乏经验,但我正在尝试创建一个模板函数,在"旋转"参数下评估n变量函数(参见下面的示例)并返回所有这些值的向量.
例如,对于具有函数f(x,y,z)的n = 3,返回的三元\向量应该是
< f(x,0,0),f(0,x,0),f(0,0,x)>
我需要的天真版本可能如下所示(不必正确\工作)
typedef FunctionSignature Function;
template<class Function, size_t Dimensions>
std::array<Function::Out,Dimensions> F(Function::InComponent x)
{
std::array<Function::Out,Dimensions> Result;
for (i=0; i<Dimensions; i++)
Result[i] = Function::f("rotate((x,0,...,0),i)");
return Result;
}
Run Code Online (Sandbox Code Playgroud)
但是如何做到这rotate一点.
我也希望运行时for可以以某种方式被消除,因为n在编译时众所周知.