考虑以下功能:
Widget f(Widget w) {
return w;
}
Run Code Online (Sandbox Code Playgroud)
假设Widget根据C++标准实现复制和移动构造函数,w必须在return语句中将其视为rvalue对象,以防编译器不认为复制省略是更好的选择.
另一方面,请考虑以下版本:
Widget f(Widget&& w) {
return w;
}
Run Code Online (Sandbox Code Playgroud)
至于对面的第一个版本,根据Item 25的Effective Modern C++,笔者似乎暗示回来w肯定会调用拷贝构造函数.换句话说,他建议返回std::move(w),以使编译器使用(可能更快)移动构造函数.
你能解释为什么第二版f()采取Widget&&的说法是不等同于第一个版本采取了Widget按值相对于构造被称为return语句,也考虑到在两者的功能体的表达w是指lvalue?
演示行为的完整示例:
#include <iostream>
struct Widget
{
Widget() { std::cout << "constructed" << std::endl; }
~Widget() { std::cout << "destructed" << std::endl; }
Widget(const Widget&) { std::cout << "copy-constructed" << std::endl; }
Widget(Widget&&) { std::cout …Run Code Online (Sandbox Code Playgroud) 拥有以下相当简单的代码和正确配置的基于JTA的持久化上下文:
abstract class AbstractRepository<E> {
@PersistenceContext
protected EntityManager em;
@Transactional
public synchronized void persist(E entity) {
em.persist(entity);
em.flush();
}
}
@ApplicationScoped
class MyEntityRepository extends AbstractRepository<MyEntity> {
}
Run Code Online (Sandbox Code Playgroud)
我在调用时遇到以下异常MyEntityRepository.persist():
2015-06-23T12:34:55.233+0200|Severe: javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
at my.project.AbstractRepository.persist(AbstractRepository.java:28)
at my.project.QuestionnaireRepository.persist(QuestionnaireRepository.java:1)
at my.project.QuestionnaireRepository$Proxy$_$$_WeldClientProxy.persist(Unknown Source)
at my.project.QuestionnaireForm.save(QuestionnaireForm.java:29)
at my.project.QuestionnaireForm.lambda$0(QuestionnaireForm.java:1)
at my.project.QuestionnaireForm$$Lambda$56/1079229220.buttonClick(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) …Run Code Online (Sandbox Code Playgroud) 那么,标准无法保证inline函数实际内联; 必须使用宏才能获得100%的保证.无论inline关键字如何,编译器总是根据自己的规则决定哪个函数内联或不内联.
那么,inline当使用现代编译器(如最新版本的GCC)时,关键字何时会对编译器的作用产生什么影响?
库基础知识的C++扩展,版本2(N4564)介绍了该类型std::experimental::source_location.
§14.1.2[reflection.src_loc.creation]说:
Run Code Online (Sandbox Code Playgroud)static constexpr source_location current() noexcept;返回:当函数调用(C++14§5.2.2)调用其后缀表达式(可能是带括号的)id-expression命名时
current,返回source_location带有实现定义值的a.该值应受#line(C++14§16.4)影响,其方式与__LINE__和__FILE__.如果以其他方式调用,则返回的值未指定.备注:当使用大括号或等于初始化程序来初始化非静态数据成员时,任何调用都
current应该对应于构造函数的位置或初始化成员的聚合初始化.[ 注意:当用作默认参数(C++14§8.3.6)时,该值
source_location将是current呼叫站点呼叫的位置.- 结束说明 ]
如果我理解正确,那么该功能就像这样使用.
#include <experimental/source_location> // I don't actually have this header
#include <iostream>
#include <string>
#include <utility>
struct my_exception
{
std::string message {};
std::experimental::source_location location {};
my_exception(std::string msg,
std::experimental::source_location loc = std::experimental::source_location::current()) :
message {std::move(msg)},
location {std::move(loc)}
{ …Run Code Online (Sandbox Code Playgroud) 我一直在寻找在C++中在堆栈或堆上分配对象的经验法则.我在这里找到了许多关于SO的讨论.很多人说,这是关于物体的寿命.如果您需要比函数范围更长的生命周期,请将其放入堆中.这很有道理.
但令我困惑的是,许多人说,如果它们很小,就会将对象分配给堆栈.如果对象很大,请将其放入堆中.但他们都没有说如何识别物体是否很大?
我有以下问题,
vector<string>.它将有大约100件物品.如果我将这个类分配给堆栈,它是否会导致堆栈溢出?我试过这个,但效果很好.不确定我做错了什么.在NetBeans中编程时,我下载了所有正确的编译器.C对我来说很好.
但是现在我开始使用C++,我意识到在打开一个带有扩展.cpp的新源文件C++时,NetBeans会转到C编译器,然后找不到包含<iostream>等等.
但是当使用扩展打开文件时,.c++NetBeans会转到正确的目录并确实识别C++相关的所有内容.有没有办法改变它,以便扩展.cpp也将转到正确的目录?谢谢!
我大多说服自己,我遇到了一些g ++ 4.8.3错误,但我想我会首先问这个列表,因为我对setjmp/longjmp的经验很少.我将我的代码简化为以下foo.cxx:
#include <setjmp.h>
#include <string.h>
// Changing MyStruct to be just a single int makes the compiler happy.
struct MyStruct
{
int a;
int b;
};
// Setting MyType to int makes the compiler happy.
#ifdef USE_STRUCT
typedef MyStruct MyType;
#elif USE_INT
typedef int MyType;
#endif
void SomeFunc(MyType val)
{
}
static void static_func(MyType val)
{
SomeFunc(val);
}
int main(int argc, char **argv)
{
jmp_buf env;
if (setjmp(env))
{
return 1;
}
MyType val;
#ifdef USE_STRUCT
val.a = …Run Code Online (Sandbox Code Playgroud) 我在Java 8中进行探索TemporalQuery和TemporalAccessor介绍TemporalAccessor似乎是专门为时间对象设计的,例如日期,时间,偏移或这些的某种组合.什么是时间对象?一些谷歌搜索给出了它的含义
随时间变化的对象
但是无法在Java的上下文中将其与此相关联?
我看到一些 bash 脚本使用$1, $2, ... 来访问在命令行上传递的参数,而一些脚本使用${1}, ${2}, ... 。这两种语法有什么区别?
根据我的理解,引入C++ C++版本(例如stdlib.h(cstdlib))的原因之一是全局命名空间不会受到污染.
但事实证明,malloc即使我没有,我也可以在全局命名空间中使用#include <stdlib.h>.
那么我为什么要#include <cstdlib>使用std::malloc呢?
(我正在使用g ++版本4.8.2)