Skip to content

Commit ce3d18d

Browse files
committed
Определение stream insertion оператора “<<” для объединения.
1 parent fefad49 commit ce3d18d

File tree

12 files changed

+277
-0
lines changed

12 files changed

+277
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
- [Анонимное объединение внутри структуры с отслеживанием активного элемента](syncfusion/cpp_succinctly/UnionSample)
7777
- [Передача объединения в функцию](union/pass-union-to-function)
7878
- [Объединение указателей](union/union-of-ptr)
79+
- [Определение stream insertion оператора “<<” для объединения](union/union-and-stream-insertion-operator)
7980

8081
## Классы
8182

union/union-and-stream-insertion-operator/.gitignore

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# ---------------------------------------------------------------------------
2+
# https://github.com/github/gitignore/blob/master/C++.gitignore
3+
4+
# Prerequisites
5+
*.d
6+
7+
# Compiled Object files
8+
*.slo
9+
*.lo
10+
*.o
11+
*.obj
12+
13+
# Precompiled Headers
14+
*.gch
15+
*.pch
16+
17+
# Compiled Dynamic libraries
18+
*.so
19+
*.dylib
20+
*.dll
21+
22+
# Fortran module files
23+
*.mod
24+
*.smod
25+
26+
# Compiled Static libraries
27+
*.lai
28+
*.la
29+
*.a
30+
*.lib
31+
32+
# Executables
33+
*.exe
34+
*.out
35+
*.app
36+
37+
# ---------------------------------------------------------------------------
38+
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore
39+
40+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
41+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
42+
43+
# User-specific stuff
44+
.idea/**/workspace.xml
45+
.idea/**/tasks.xml
46+
.idea/**/usage.statistics.xml
47+
.idea/**/dictionaries
48+
.idea/**/shelf
49+
50+
# AWS User-specific
51+
.idea/**/aws.xml
52+
53+
# Generated files
54+
.idea/**/contentModel.xml
55+
56+
# Sensitive or high-churn files
57+
.idea/**/dataSources/
58+
.idea/**/dataSources.ids
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
.idea/**/dbnavigator.xml
64+
65+
# Gradle
66+
.idea/**/gradle.xml
67+
.idea/**/libraries
68+
69+
# Gradle and Maven with auto-import
70+
# When using Gradle or Maven with auto-import, you should exclude module files,
71+
# since they will be recreated, and may cause churn. Uncomment if using
72+
# auto-import.
73+
# .idea/artifacts
74+
# .idea/compiler.xml
75+
# .idea/jarRepositories.xml
76+
# .idea/modules.xml
77+
# .idea/*.iml
78+
# .idea/modules
79+
# *.iml
80+
# *.ipr
81+
82+
# CMake
83+
cmake-build-*/
84+
85+
# Mongo Explorer plugin
86+
.idea/**/mongoSettings.xml
87+
88+
# File-based project format
89+
*.iws
90+
91+
# IntelliJ
92+
out/
93+
94+
# mpeltonen/sbt-idea plugin
95+
.idea_modules/
96+
97+
# JIRA plugin
98+
atlassian-ide-plugin.xml
99+
100+
# Cursive Clojure plugin
101+
.idea/replstate.xml
102+
103+
# SonarLint plugin
104+
.idea/sonarlint/
105+
106+
# Crashlytics plugin (for Android Studio and IntelliJ)
107+
com_crashlytics_export_strings.xml
108+
crashlytics.properties
109+
crashlytics-build.properties
110+
fabric.properties
111+
112+
# Editor-based Rest Client
113+
.idea/httpRequests
114+
115+
# Android studio 3.1+ serialized cache file
116+
.idea/caches/build_file_checksums.ser
117+
118+
# ---------------------------------------------------------------------------
119+
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
120+
121+
# General
122+
.DS_Store
123+
.AppleDouble
124+
.LSOverride
125+
126+
# Icon must end with two \r
127+
Icon
128+
129+
# Thumbnails
130+
._*
131+
132+
# Files that might appear in the root of a volume
133+
.DocumentRevisions-V100
134+
.fseventsd
135+
.Spotlight-V100
136+
.TemporaryItems
137+
.Trashes
138+
.VolumeIcon.icns
139+
.com.apple.timemachine.donotpresent
140+
141+
# Directories potentially created on remote AFP share
142+
.AppleDB
143+
.AppleDesktop
144+
Network Trash Folder
145+
Temporary Items
146+
.apdisk
147+
148+
# ---------------------------------------------------------------------------
149+
# https://github.com/github/gitignore/blob/master/CMake.gitignore
150+
151+
CMakeLists.txt.user
152+
CMakeCache.txt
153+
CMakeFiles
154+
CMakeScripts
155+
Testing
156+
Makefile
157+
cmake_install.cmake
158+
install_manifest.txt
159+
compile_commands.json
160+
CTestTestfile.cmake
161+
_deps
162+
163+
# ---------------------------------------------------------------------------

union/union-and-stream-insertion-operator/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

union/union-and-stream-insertion-operator/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

union/union-and-stream-insertion-operator/.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

union/union-and-stream-insertion-operator/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

union/union-and-stream-insertion-operator/.idea/union-and-stream-insertion-operator.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

union/union-and-stream-insertion-operator/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(UnionAndStreamInsertionOperator)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(${PROJECT_NAME} main.cpp foo.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#include <type_traits>
3+
4+
union _Vector3
5+
{
6+
struct { float x, y, z; };
7+
8+
struct { float r, g, b; };
9+
10+
float v[3];
11+
12+
_Vector3(){v[0] = 0.0f; v[1] = 0.0f; v[2] = 0.0f;}
13+
14+
// Из-за этих двух конструкторов при выводе символа в std::stringstream получим ошибку компиляции:
15+
// Use of overloaded operator '<<' is ambiguous (with operand types 'std::stringstream' (aka 'basic_stringstream<char>') and 'char')
16+
_Vector3(int _v){v[0] = (float)_v; v[1] = (float)_v; v[2] = (float)_v;}
17+
_Vector3(float _v){v[0] = _v; v[1] = _v; v[2] = _v;}
18+
19+
_Vector3(float x, float y, float z){v[0] = x; v[1] = y; v[2] = z;}
20+
};
21+
22+
typedef union _Vector3 Vector3;
23+
24+
static_assert(std::is_same_v<Vector3, _Vector3>);
25+
26+
static_assert(!std::is_same_v<Vector3, float[]>);
27+
static_assert(!std::is_same_v<Vector3, float[3]>);
28+
static_assert(!std::is_same_v<Vector3, float*>);
29+
30+
/*
31+
template<typename TOutputStream>
32+
TOutputStream& operator<<(TOutputStream& aOutputStream, Vector3 const& aVector3)
33+
{
34+
aOutputStream << "(" << aVector3.x << ", " << aVector3.y << ", " << aVector3.z << ")";
35+
return aOutputStream;
36+
}
37+
//*/
38+
39+
//* FIX:
40+
template<typename TOutputStream, typename T>
41+
typename std::enable_if<std::is_same<T, Vector3>::value, TOutputStream&>::type
42+
operator<<(TOutputStream& aOutputStream, T const& aVector3)
43+
{
44+
aOutputStream << "(" << aVector3.x << ", " << aVector3.y << ", " << aVector3.z << ")";
45+
return aOutputStream;
46+
}
47+
//*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
#include "Vector3.h"
3+
4+
void foo()
5+
{
6+
Vector3 v;
7+
std::cout << v;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <sstream>
4+
#include "Vector3.h"
5+
6+
int main()
7+
{
8+
9+
std::string hello("Hello, World!");
10+
std::stringstream strstream;
11+
for (auto it = hello.begin(); it != hello.end(); ++it)
12+
strstream << *it;
13+
strstream << std::endl;
14+
std::cout << strstream.str();
15+
16+
Vector3 v;
17+
std::cout << v << std::endl;
18+
19+
float f = 2249.f;
20+
std::cout << f << std::endl;
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)