例:
#include <functional>
int main() {
auto test = []{};
test = []{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会在gcc 4.7.2中发出以下错误消息:
test.cpp: In function ‘int main()’:
test.cpp:5:13: error: no match for ‘operator=’ in ‘test = <lambda closure object>main()::<lambda()>{}’
test.cpp:5:13: note: candidate is:
test.cpp:4:16: note: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
test.cpp:4:16: note: no known conversion for argument 1 from ‘main()::<lambda()>’ to ‘const main()::<lambda()>&’
Run Code Online (Sandbox Code Playgroud)
从标准5.1.2.3(强调我的):
实现可以定义闭包类型与下面描述的不同,前提是这不会改变程序的可观察行为,只需更改:
- 封闭类型的大小和/或对齐方式,
- 封闭类型是否可以轻易复制(第9条)
- 闭包类型是否为标准布局类(第9条),或
- 闭包类型是否为POD类(第9条).
据我所知,这就是我遇到的情况.它试图使用已删除的赋值运算符并失败.我很想知道是否有一个简单的解决方法,更广泛地说,一般来说,为lambdas省略复制可构造性的动机理由是什么.
只是为了好玩,因为它非常简单,我编写了一个简短的程序来生成嫁接数,但由于浮点精度问题,它没有找到一些更大的例子.
def isGrafting(a):
for i in xrange(1, int(ceil(log10(a))) + 2):
if a == floor((sqrt(a) * 10**(i-1)) % 10**int(ceil(log10(a)))):
return 1
a = 0
while(1):
if (isGrafting(a)):
print "%d %.15f" % (a, sqrt(a))
a += 1
Run Code Online (Sandbox Code Playgroud)
此代码缺少至少一个已知的嫁接编号.9999999998 => 99999.99998999999999949999999994999999999374999999912...
它乘以后似乎会降低额外的精度10**5
.
>>> a = 9999999998
>>> sqrt(a)
99999.99999
>>> a == floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
False
>>> floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
9999999999.0
>>> print "%.15f" % sqrt(a)
99999.999989999996615
>>> print "%.15f" % (sqrt(a) * 10**5) …
Run Code Online (Sandbox Code Playgroud) 通常,您可以使用以下语法为类定义强制转换:
class Test {
public:
explicit operator bool() { return false; }
};
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点或类似的东西enum class
?
Tomcat版本:7.0.20
我正在尝试通过以下Spring MVC教程:http: //static.springsource.org/docs/Spring-MVC-step-by-step/part1.html
在本教程中,将使用管理器设置ant构建脚本以部署到tomcat.但是,当我尝试运行任何tomcat任务时,我遇到了一些问题.
首先,在教程中,他们仍然使用org.apache.catalina.ant.InstallTask
已弃用的,所以我改为org.apache.catalina.ant.DeployTask
.
现在的问题是,当我尝试运行Tomcat任务时,我得到:
java.lang.NoClassDefFoundError: org/apache/tomcat/util/buf/B2CConverter
at org.apache.catalina.util.Base64.encode(Base64.java:177)
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:204)
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:150)
at org.apache.catalina.ant.ReloadTask.execute(ReloadTask.java:45)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.util.buf.B2CConverter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 21 more …
Run Code Online (Sandbox Code Playgroud) 我正在更新我的结构,我想要添加一个std :: string成员.原始结构如下所示:
struct Value {
uint64_t lastUpdated;
union {
uint64_t ui;
int64_t i;
float f;
bool b;
};
};
Run Code Online (Sandbox Code Playgroud)
当然,只是将一个std :: string成员添加到union会导致编译错误,因为通常需要添加该对象的非平凡构造函数. 在std :: string的情况下(来自informit.com的文本)
由于std :: string定义了所有六个特殊成员函数,因此U将具有隐式删除的默认构造函数,复制构造函数,复制赋值运算符,移动构造函数,移动赋值运算符和析构函数.实际上,这意味着除非您明确定义某些或所有特殊成员函数,否则无法创建U的实例.
然后该网站继续提供以下示例代码:
union U
{
int a;
int b;
string s;
U();
~U();
};
Run Code Online (Sandbox Code Playgroud)
但是,我在结构中使用匿名联合.我在freenode上问了## C++,他们告诉我这样做的正确方法是将构造函数放在struct中,并给我这个示例代码:
#include <new>
struct Point {
Point() {}
Point(int x, int y): x_(x), y_(y) {}
int x_, y_;
};
struct Foo
{
Foo() { new(&p) Point(); }
union {
int z;
double w;
Point p;
};
}; …
Run Code Online (Sandbox Code Playgroud) 我想做类似以下的事情:
//std::vector<std::pair<TypeA, TypeB>> someInitializingFunction();
{
TypeA a;
TypeB b;
for (std::tie(a, b) : someInitializingFunction()) {
// do stuff;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不是有效的代码,因为正如标准所说,基于for循环的范围被定义为等效于:
{
auto && __range = range-init;
for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin ) {
for-range-declaration = *__begin;
statement
}
}
Run Code Online (Sandbox Code Playgroud)
其中for-range-declaration定义为:
for-range-declaration:attribute-specifier-seq_ {opt} decl-specifier-seq声明符
那么阻碍我的是decl-specifier-seq没有标记为可选的?
因此,似乎我必须依靠旧式的循环来实现这个问题:
std::vector<std::pair<TypeA, TypeB>> myList = someInitializingFunction();
{
TypeA a;
TypeB b;
for (auto it = myList.begin(); it != myList.end(); ++it) {
std::tie(a, b) = *it;
// do …
Run Code Online (Sandbox Code Playgroud) 我有这个和一个简单的问题.
$.ajax({
type: "POST",
url: "/",
data: $(".form").serialize(),
dataType: "html",
success: function (data) {
$("#id").html(data);
}
});
Run Code Online (Sandbox Code Playgroud)
里面的"数据"是我插入DOM的一些html.那没问题.但是我想在这之前操纵"数据".我怎样才能做到这一点?例如,"data"中有一些li元素.例如,在将数据插入DOM之前,我将如何删除"data"字符串中的最后一个li元素?
我试过类似的东西
$(data li:last)remove();
Run Code Online (Sandbox Code Playgroud)
......但那没有用.
谢谢你的帮助.
在这个例子中:
template<class T>
struct S : T
{
using T::X;
};
Run Code Online (Sandbox Code Playgroud)
T::X
是指部件上的从属名称X
在T
.如果S<T>
实例化为T = X
:
struct X
{
X(int) {}
};
...
S<X> s(42);
Run Code Online (Sandbox Code Playgroud)
using声明是否会成为继承构造函数?
Clang拒绝代码DEMO,而g ++接受它.
请注意,如果我们写:
using T::X::X;
Run Code Online (Sandbox Code Playgroud)
两个编译器都接受代码并将其视为继承构造函数.被using T::X
允许通过标准成为继承,构造函数?
c++ templates using-declaration language-lawyer inheriting-constructors
I am referring to the following python code
all(a==2 for a in my_list)
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码返回True,如果my_list中的所有元素都是2.但是当我将my_list设为空并将其作为
my_list = []
all(a==2 for a in my_list)
Run Code Online (Sandbox Code Playgroud)
它也返回True.我对这种行为感到困惑.是不是应该返回False,因为my_list中没有值为2的元素?
我相信我发现了gcc的别名模板处理问题.实质上,当通过引用引用类型时,gcc似乎无法正确地将别名的template-id替换为别名模板实例化.
我能够将一个混乱的现实问题简化为C++ 11标准部分temp.alias(14.5.7/2)中提供的非规范示例的微小变化:
#include <vector>
using namespace std;
template <class T>
using Vec = vector<T, allocator<T>>;
template <template <class> class TT>
void f1(TT<int> v);
template <template <class> class TT>
void f2(TT<int>& v);
template <template <class, class> class TT>
void g1(TT<int, allocator<int>> v);
template <template <class, class> class TT>
void g2(TT<int, allocator<int>>& v);
void foo()
{
Vec<int> v;
f1(v); // gcc and clang both correctly yield no matching function error
g1(v);
f2(v); // clang yields a no matching function error
g2(v); …
Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×4
python ×2
templates ×2
anonymous ×1
ant ×1
constructor ×1
enum-class ×1
enums ×1
for-loop ×1
gcc ×1
java ×1
jquery ×1
lambda ×1
python-2.7 ×1
spring-mvc ×1
std-pair ×1
tie ×1
tomcat ×1
unions ×1