optional

std::optional - construct empty with {} or std::nullopt?

戏子无情 提交于 2020-01-12 06:28:11
问题 I thought that initializing a std::optional with std::nullopt would be the same as default construction. They are described as the same at cppreference, as form (1) However, both Clang and GCC seem to treat these toy example functions differently. #include <optional> struct Data { char large_data[0x10000]; }; std::optional<Data> nullopt_init() { return std::nullopt; } std::optional<Data> default_init() { return {}; } Compiler Explorer seems to imply that using std::nullopt will simply set the

std::optional - construct empty with {} or std::nullopt?

♀尐吖头ヾ 提交于 2020-01-12 06:28:08
问题 I thought that initializing a std::optional with std::nullopt would be the same as default construction. They are described as the same at cppreference, as form (1) However, both Clang and GCC seem to treat these toy example functions differently. #include <optional> struct Data { char large_data[0x10000]; }; std::optional<Data> nullopt_init() { return std::nullopt; } std::optional<Data> default_init() { return {}; } Compiler Explorer seems to imply that using std::nullopt will simply set the

Different results in Clang and GCC when casting to std::optional<T>

拟墨画扇 提交于 2020-01-12 06:26:10
问题 Given the following code: #include <iostream> #include <optional> struct foo { explicit operator std::optional<int>() { return std::optional<int>( 1 ); } explicit operator int() { return 0; } }; int main() { foo my_foo; std::optional<int> my_opt( my_foo ); std::cout << "value: " << my_opt.value() << std::endl; } gcc 7.2.0 writes value: 1 . MSVC 2017 (15.3) and clang 4.0.0 however write value: 0 . Which one is correct according to the C++ standard? 回答1: Since this is direct-initialization, we

GC overhead of Optional<T> in Java

这一生的挚爱 提交于 2020-01-11 09:50:08
问题 We all know that every object allocated in Java adds a weight into future garbage collection cycles, and Optional<T> objects are no different. We use these objects frequently to wrap nullable, which leads to safer code, but at what cost? Does anyone have information on what kind of additional GC pressure optional objects add vs. simply returning nulls and what kind of impact this has on performance in high-throughput systems? 回答1: We all know that every object allocated in Java adds a weight

Throw exception if Optional<> value is present

老子叫甜甜 提交于 2020-01-11 08:53:09
问题 Suppose I have a Spring Data Repository method. Optional<Branch> findByName(@Nonnull final String name); My business logic is such if I find any value for this method execution I would throw an exception. I could do this for example : Optional.of(branchRepository.findByName(branch.getName())) .filter(bo -> !bo.isPresent()) .orElseThrow(NameNotAvailableException::new); or another way: Optional.of(branchRepository.findByName(branch.getName())) .filter(Optional::isEmpty) .orElseThrow

Is there a real reason to use Optional.of()?

喜欢而已 提交于 2020-01-11 04:43:13
问题 I've read here why Optional.of() should be used over Optional.ofNullable() , but the answer didn't satisfy me at all, so I ask slightly different: If you are SURE that your method does not return null , why should you use Optional at all? As far as I know, the more or less only purpose of it is to remind the "user of a method", that he might have to deal with null -values. If he does not have to deal with null -values, why should he be bothered with an Optional ? I ask, because I recently

Return value from Optional [closed]

元气小坏坏 提交于 2020-01-11 04:13:04
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . How to return a String value from an Optional<String> using ifPresent and avoiding NullPointerException ? Example: public String longestName() { Optional<String> longName = someList.stream().reduce((name1, name2) -> name1.length() > name2.length() ? name1 : name2); // If I do not want to use

Evaluate Bool property of optional object in if statement

孤街浪徒 提交于 2020-01-10 14:20:51
问题 I am looking for a way to evaluate a Swift Bool concisely in a single if statement, when the Bool is the property of an optional object: var objectWithBool: ClassWithBool? // ... if let obj = objectWithBool { if obj.bool { // bool == true } else { // bool == false } } else { // objectWithBool == nil } Is there are way to combine these if statements? In Objective-C this could easily be done, as a nil object can be evaluated in the same expression: if (objectWithBool.bool) { // bool == true }

MAVEN POM dependencies and Dependency Exclusions

别说谁变了你拦得住时间么 提交于 2020-01-10 13:10:52
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Introduction This section discusses the functionality of optional dependencies and dependency exclusions. This will help users to understand what are they, how to use them, how they work and when is the best way to use them. It also explains why exclusions are made as per dependency basis not in a POM level. Optional Dependencies Optional dependencies are used when it's not really possible (for whatever reason) to split a project up into sub-modules. The idea is that some of the dependencies are only used for certain features in the project, and will not be

Why does Swift's optional binding succeed with 'nil' in certain cases?

故事扮演 提交于 2020-01-10 04:30:07
问题 Apple's Swift language documentation says that optional binding (a.k.a. if let ) will "check for a value inside an optional" and " extract that value into" a variable or constant). But this doesn't match what I'm seeing. For example var x: Int? = nil if let y1: Int? = x { println("y1 = \(y1)") // This is printed, suggesting that x is not checked "inside", but left as Optional(nil) (!= nil) } if let y2: Int? = x? { println("y2 = \(y2)") } if let y3: Int = x? { println("y3 = \(y3)") } if let y4