LoginSignup
0
0

More than 1 year has passed since last update.

C++マクロからtemplateへ<エンジニア夏休み企画> 【個人開発】

Last updated at Posted at 2022-09-03

エンジニア夏休み企画 個人開発

個人開発 

個人開発のまずいところが時々露呈する。
 
何か、作業を始めたが、お金にならないと、途中で止まったまま。 
作業記録はつける習慣はあるが、今後どうしたいかは、その時書いても98%そうならないので、どうしたいかを書かない癖がついてしまった。
 
4年前のやりかけの記録を再現し、年内に整理をつけたい。

C++マクロからtemplateへ 2018/06/22 小川 清

書き直し

1 マクロ v.s. Template

mac.cpp
#define MULTIPLY(a,b) (a)*(b)

上記マクロは下記templateで代替可能らしい

tem.cpp
template<typename T> T multiply(T a, T b) {
return a * b;
}

int val = multiply<int>(10 * 20);

コンパイルしてみた。

bash
root@9d41f17b39c5:/Users/ogawakiyoshi/n4910# gcc tem.cpp    
tem.cpp: In function 'int main()':
tem.cpp:6:24: error: no matching function for call to 'multiply<int>(int)'
    6 | int val = multiply<int>(10 * 20);
      |           ~~~~~~~~~~~~~^~~~~~~~~
tem.cpp:2:24: note: candidate: 'template<class T> T multiply(T, T)'
    2 | template<typename T> T multiply(T a, T b) {
      |                        ^~~~~~~~
tem.cpp:2:24: note:   candidate expects 2 arguments, 1 provided
tem.cpp:7:1: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
    7 | cout << val;
      | ^~~~
      | std::cout
In file included from tem.cpp:1:

using namespaceとincludeを書いた。

tem.cpp
#include <iostream>
template<typename T> T multiply(T a, T b) {
return a * b;
}
using namespace std;
int main(){
int val = multiply<int>(10 * 20);
cout << val;
}

再度build

bash
# gcc tem.cpp
tem.cpp: In function 'int main()':
tem.cpp:7:24: error: no matching function for call to 'multiply<int>(int)'
    7 | int val = multiply<int>(10 * 20);
      |           ~~~~~~~~~~~~~^~~~~~~~~
tem.cpp:2:24: note: candidate: 'template<class T> T multiply(T, T)'
    2 | template<typename T> T multiply(T a, T b) {
      |                        ^~~~~~~~
tem.cpp:2:24: note:   candidate expects 2 arguments, 1 provided

関数が定義と利用でちゃうやん。

tem.cpp
#include <iostream>
template<typename T> T multiply(T a, T b) {
return a * b;
}
using namespace std;
int main(){
int val = multiply<int>(10 , 20);
cout << val;
}

直した。 

bash
# gcc tem.cpp
/usr/bin/ld: /tmp/cctousUC.o: in function `main':
tem.cpp:(.text+0x20): undefined reference to `std::cout'
/usr/bin/ld: tem.cpp:(.text+0x25): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: /tmp/cctousUC.o: in function `__static_initialization_and_destruction_0(int, int)':
tem.cpp:(.text+0x53): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: tem.cpp:(.text+0x62): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

しまった。gccじゃなくてg++やん。

# g++ tem.cpp
# ./a.out
200

2 マクロ関数よりもテンプレート関数

macrofunc.cpp
#define MACRO_MAX(a,b) ((a)>(b)?(a):(b))

// テンプレート&インラインでのmax関数

template.cpp
template <class T> inline T template_max(const T& a,const T& b) {
    return a > b ? a : b;
}

3 特注の鋳型

4 よく使って来たマクロ

printf.cpp
#define  PR1x(a,b) (void)printf(" "#a  " = %" #b "=%x\n", a,b)

置き換え(まだ終わっていない)

cout.cpp
template<type T> T void pr(T a,T b) {
cout << (a) << (b);
}

どうしたらいいかわかっていない。

ふりかえり 

わかったこと 

個人開発で金にならないことは中途半端なところでほったらかしの宝庫。 

わからなかったこと 

2018年になんで、この中途半端なところでほったらかしてコンパイルしなかったか。

よかったこと 

コンパイルできたこと。 
操作間違いを記録できたこと。 

あらためること。 

ソースを書いたら、コンパイルしてからネットにあげる。または、ネットにあげたら、その日中にコンパイルする。 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0